简体   繁体   English

如何在 SAS 企业指南中的 PROC SQL 中创建标志通知 ID 上的两列是否有变化?

[英]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”:

  • If some ID never changed the value PR from column "val1" to the value of M or P in column "val2" then this ID has 1 else 0如果某个 ID 从未将值 PR 从列“val1”更改为列“val2”中的 M 或 P 的值,则此 ID 为 1,否则为 0

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:因为:

  • 123 - has 1 in "col1" because has never changed PR to M or P 123 - 在“col1”中有 1,因为从未将 PR 更改为 M 或 P
  • 444 - has 1 in "col1" because has never changed PR to M or P 444 - 在“col1”中有 1,因为从未将 PR 更改为 M 或 P
  • 567 - has 0 because changed PR to M 567 - 为 0,因为将 PR 更改为 M
  • 99 - has 0 because changed PR to P 99 - 为 0,因为将 PR 更改为 P

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.

相关问题 如何在 SAS 企业指南中的 PROC SQL 的两列之间创建标志 0/1 通知是否在 4 个月内更改? - How to create flag 0/1 inform whether was changed or not during 4 months between two columns in PROC SQL in SAS Enterprise Guide? 如何在 SAS 企业指南中的 PROC SQL 的某些日期中的 2 列之间更改值来创建 0/1 标志? - How to create 0/1 flag witch infrom whether values were changed between 2 column in some dates in PROC SQL in SAS Enterprise Guide? 如何计算在 SAS 企业指南中的 PROC SQL 中定义 ID 的某个值出现了多少次? - How to count how many time some value appeard with defined ID in PROC SQL in SAS Enterprise Guide? 如何使用 SAS 企业指南中 PROC SQL / SAS 代码中的其他 2 列中的值填充列? - How to fill column using values in 2 other columns in PROC SQL / SAS code in SAS Enterprise Guide? 如何在 SAS 企业指南中仅使用 PROC SQL select char 变量? - How to select only char variables with PROC SQL in SAS Enterprise Guide? 如何在 PROC SQL 中使用 SAS 企业指南中每月值的总和创建新列? - How to create new column in PROC SQL with sum of values per month in SAS enterprise Guide? SAS Enterprise Guide proc sql - 使用 when 语句包含在内 - SAS Enterprise Guide proc sql - using when statements inclusively SAS企业指南/ SQL性能 - SAS Enterprise Guide / SQL Performance 如何使用 SQL 中一列的值创建列(SAS 中的 PROC SQL) - How to create columns using values from one column in SQL (PROC SQL in SAS) 如何提高SAS Enterprise Guide 4的性能 - How to improve the performance of SAS Enterprise Guide 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM