[英]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.