SAS: PROC SQL 单个 CASE 导致的多个赋值
SAS: PROC SQL Multiple assignments as a consequence of a single CASE
我想知道是否可以通过单个 CASE 执行多个赋值。也就是说,不是有两个 CASE 语句,而是有一个具有类似 'then-do-end' 结构的 CASE 语句。
例如,我如何在单个 CASE 语句中根据 x
为 thing1
和 thing2
赋值?
data example;
input x $;
datalines;
A
A
A
B
B
;
run;
proc sql;
create table make_two_from_one as
select *
, case
when x = 'A' then 'Result1A'
when x = 'B' then 'Result1B'
else 'Error'
end as thing1
, case
when x = 'A' then 'Result2A'
when x = 'B' then 'Result2B'
else 'Error'
end as thing2
from example
;
quit;
Case语句构造一个变量。
对于您的示例,您可以尝试使用一个 Case 语句:
data example;
input x $;
datalines;
A
A
A
B
B
;
run;
proc sql;
create table make_two_from_one_2 as
select *
, case
when x = 'A' then 'Result1A,Result2A'
when x = 'B' then 'Result1B,Result2B'
else 'Error'
end as thing0
from example
;
quit;
data example1(drop=thing0);
set make_two_from_one_2;
thing1=scan(thing0,1,',');
thing2=scan(thing0,2,',');
run;
对于你的情况,也许你可以试试这个:
proc sql;
create table make_two_from_one as
select *
, case
when x = 'A' then 'Result1A'
when x = 'B' then 'Result1B'
else 'Error'
end as thing1,
translate(calculated thing1,'2','1') as thing2
from example
;
quit;
我个人认为这种需要,即用可以在一个地方维护的东西来克服繁琐的语法障碍,是 SAS 宏的一个很好的用例。
宏版本还避免了解析的风险,表格形式很好,并且不做任何类型假设。
%macro _case(when_A, when_B, error='Error');
case
when x = 'A' then &when_A
when x = 'B' then &when_B
else &error
end
%mend;
proc sql;
create table make_two_from_one_v2 as
select *
, %_case('Result1A', 'Result1B') as thing1
, %_case('Result2A', 'Result2B') as thing2
from example
;
quit;
为了完整起见,这在数据步骤中很简单,使用 select
、when
、do
、otherwise
。或者您可以只使用 if
、then
、do
、else
.
data want;
set example;
select (x);
when ('A') do;
thing1 = 'Result1A';
thing2 = 'Result2A';
end;
when ('B') do;
thing1 = 'Result1B';
thing2 = 'Result2B';
end;
otherwise do;
thing1 = 'Error';
thing2 = 'Error';
end;
end;
run;
我想知道是否可以通过单个 CASE 执行多个赋值。也就是说,不是有两个 CASE 语句,而是有一个具有类似 'then-do-end' 结构的 CASE 语句。
例如,我如何在单个 CASE 语句中根据 x
为 thing1
和 thing2
赋值?
data example;
input x $;
datalines;
A
A
A
B
B
;
run;
proc sql;
create table make_two_from_one as
select *
, case
when x = 'A' then 'Result1A'
when x = 'B' then 'Result1B'
else 'Error'
end as thing1
, case
when x = 'A' then 'Result2A'
when x = 'B' then 'Result2B'
else 'Error'
end as thing2
from example
;
quit;
Case语句构造一个变量。
对于您的示例,您可以尝试使用一个 Case 语句:
data example;
input x $;
datalines;
A
A
A
B
B
;
run;
proc sql;
create table make_two_from_one_2 as
select *
, case
when x = 'A' then 'Result1A,Result2A'
when x = 'B' then 'Result1B,Result2B'
else 'Error'
end as thing0
from example
;
quit;
data example1(drop=thing0);
set make_two_from_one_2;
thing1=scan(thing0,1,',');
thing2=scan(thing0,2,',');
run;
对于你的情况,也许你可以试试这个:
proc sql;
create table make_two_from_one as
select *
, case
when x = 'A' then 'Result1A'
when x = 'B' then 'Result1B'
else 'Error'
end as thing1,
translate(calculated thing1,'2','1') as thing2
from example
;
quit;
我个人认为这种需要,即用可以在一个地方维护的东西来克服繁琐的语法障碍,是 SAS 宏的一个很好的用例。
宏版本还避免了解析的风险,表格形式很好,并且不做任何类型假设。
%macro _case(when_A, when_B, error='Error');
case
when x = 'A' then &when_A
when x = 'B' then &when_B
else &error
end
%mend;
proc sql;
create table make_two_from_one_v2 as
select *
, %_case('Result1A', 'Result1B') as thing1
, %_case('Result2A', 'Result2B') as thing2
from example
;
quit;
为了完整起见,这在数据步骤中很简单,使用 select
、when
、do
、otherwise
。或者您可以只使用 if
、then
、do
、else
.
data want;
set example;
select (x);
when ('A') do;
thing1 = 'Result1A';
thing2 = 'Result2A';
end;
when ('B') do;
thing1 = 'Result1B';
thing2 = 'Result2B';
end;
otherwise do;
thing1 = 'Error';
thing2 = 'Error';
end;
end;
run;