简体   繁体   English

SAS:在组内按组为变量值编码一个虚拟变量

[英]SAS: Coding a dummy variable for a value of a variable by group within group

I have a dataset of CASE_ID (xy and z), a set of multiple dates (including duplicate dates) for each CASE_ID, and a variable VAR. 我有一个CASE_ID(xy和z)数据集,每个CASE_ID的一组多个日期(包括重复的日期)和一个变量VAR。 I would like to create a dummy variable DUMMYVAR by group within a group whereby if VAR="C" for CASE_ID x on some specific date, then DUMMYVAR=1 for all observations corresponding to CASE_ID x on with that date. 我想在一个组内按组创建一个虚拟变量DUMMYVAR,如果在某个特定日期,如果CASE_ID x的值是VAR =“ C”,那么该日期对应于CASE_ID x的所有观察值的值DUMMYVAR = 1。

I believe that a Classic 2XDOW would be the key here but this is my third week using SAS and having difficulty getting this by two BY groups here. 我相信Classic 2XDOW将是关键,但这是我第三周使用SAS,并且在这里很难被两个BY组获得。

I have referenced and attempted to write a variation of Haikuo's code here: 我已经参考并尝试在此处编写Haikuo代码的变体:

 PROC SORT have;
         by CASE_ID DATE;
    RUN;

    data want;
    do until (last.DATE);
      set HAVE;
       by date notsorted; 
       if var='c' then DUMMYVAR=1; 

    do until (last.DATE);
      set HAVE;
       by DATE notsorted;

       if DATE=1 then ????????

    end;
    run;

Change your BY statements to match the grouping you are doing. 更改您的BY语句以匹配您正在执行的分组。 And in the second loop add a simple OUTPUT; 在第二个循环中,添加一个简单的OUTPUT; statement. 声明。 Then your new dataset will have all the rows in your original dataset and the new variable DUMMYVAR. 然后,新数据集将具有原始数据集中的所有行以及新变量DUMMYVAR。

data want;
  do until (last.DATE);
    set HAVE;
    by case_id date; 
    if var='c' then DUMMYVAR=1; 
  end;
  do until (last.DATE);
   set HAVE;
    by case_id date; 
    output;
  end;
run;

This will create the variable DUMMYVAR with values of either 1 or missing. 这将创建值为1或缺失的变量DUMMYVAR。 If you want the values to be 1 or 0 then you could either set it to 0 before the first DO loop. 如果希望值是1或0,则可以在第一个DO循环之前将其设置为0。 Or add if first.date then dummyvar=0; 或添加if first.date then dummyvar=0;添加if first.date then dummyvar=0; statement before the existing IF statement. 在现有IF语句之前的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用现有的虚拟变量创建一个新变量,该变量对组内的某些先导观察值取值 1 - How to use an existing dummy variable to create a new one that takes the value 1 for certain lead observations within a group SAS PROC SQL-按组将变量值连接为单个值 - SAS PROC SQL - Concatenate variable values into a single value by group 组内SAS排序 - SAS Sorting within group SAS - 当组中的其他变量发生变化时增加计数变量 - SAS - Increment a counting variable increasing whenver other variable in a group changes 创建新变量,直到另一个变量的第一个非NA值均为0,此后为1(在组中) - Create new variable that is 0 until the first non-NA value of another variable, then 1 thereafter (within a group) 如果POSIXct变量缺少组的最后一个值,则替换 - Replace if last value of group is missing with a POSIXct variable 查找一个MySQL组的最大值,并将其分配给一个变量 - Find the maximum value of a MySQL group by and assign it to a variable 使用 group_by 和 summarise_all 为分类变量创建虚拟指标 - Using group_by and summarise_all to create dummy indicators for categorical variable 在组内查找值 +1 - Finding value +1 within a group 按 MySQL 中的变量 substring 分组 - Group by variable substring in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM