[英]How to create flag inform whether was some change in two columns on ID in PROC SQL in SAS Enterprise Guide?
I have table in SAS Enterprise Guide like below:我在 SAS 企业指南中有如下表:
ID | val1| val2
----|-----|-----
123 | M | M
123 | M | P
123 | P | P
444 | PR | PR
444 | PR | PR
567 | PR | M
567 | M | M
99 | PR | P
And I need to creeate new column "col1" with values 0/1:我需要创建值为 0/1 的新列“col1”:
So, as a result I need something like below:因此,我需要如下所示的内容:
ID | val1| val2| col1
----|-----|-----|----
123 | M | M | 1
123 | M | P | 1
123 | P | P | 1
444 | PR | PR | 1
444 | PR | PR | 1
567 | PR | M | 0
567 | M | M | 0
99 | PR | P | 0
Because:因为:
How can I do that in PROC SQL in SAS Enterprise Guide?如何在 SAS 企业指南中的 PROC SQL 中做到这一点?
So you want a single value at the ID level that is replicated onto all observations for that level of ID?因此,您想要将 ID 级别的单个值复制到该 ID 级别的所有观察值上吗? PROC SQL makes that easy because it will automatically remerge aggregate values back to the detailed observations. PROC SQL 使这很容易,因为它会自动将聚合值重新合并回详细观察。
It sounds like the test you want is听起来你想要的测试是
val1='PR' and val2 in ('M' 'P')
You then want the overall result to be 1 (TRUE) when that expression is never true.然后,当该表达式永远不为真时,您希望整体结果为 1 (TRUE)。
data have ;
input ID val1 $ val2 $ EXPECT ;
cards;
123 M M 1
123 M P 1
123 P P 1
444 PR PR 1
444 PR PR 1
567 PR M 0
567 M M 0
99 PR P 0
;
proc sql;
create table want as
select *
, min(not (val1='PR' and val2 in ('M' 'P'))) as COL1
from have
group by id
;
quit;
Result:结果:
Obs ID val1 val2 EXPECT COL1
1 99 PR P 0 0
2 123 P P 1 1
3 123 M M 1 1
4 123 M P 1 1
5 444 PR PR 1 1
6 444 PR PR 1 1
7 567 M M 0 0
8 567 PR M 0 0
Solution if the can be changed:如果可以更改的解决方案:
proc sort data=have;
by ID;
run;
data want;
* for every ID, read the data you have twice *;
set have (in=first_pass) have (in=last_pass);
by ID;
* From the first pass, remember if any observation "changed" from PR to P or M *;
retain col1;
if first.ID then col1 = 0;
if val1 eq 'PR' and val2 in ('P', 'M') then col1 = 1;
* only output the results from the second pass *;
if last_pass;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.