Hello guys,
hope you all are doing well.
Here is one task i need your help,
I am creating one dataset below,
data a;
input subjid cd$ d;
datalines;
2006 XY8 1
2006 XY8 1
2006 ABC 0
2006 EFG 0
2006 HIJ 0
2007 XY8 1
2008 ABC 0
2008 EFG 0
2008 HIJ 0
;
run;
so the query is, if CD variable contents XY8 value then i want to flag that whole subject. for more clear understanding please look at below table.
I want output as below,
Desired output:
SUBJID CD D FLG
2006 XY8 1 Y
2006 XY8 1 Y
2006 ABC 0 Y
2006 EFG 0 Y
2006 HIJ 0 Y
2007 XY8 1 Y
2008 ABC 0
2008 EFG 0
2008 HIJ 0
Thanks In Advance...
Here is an SQL approach
data a;
input subjid cd$ d;
datalines;
2006 XY8 1
2006 XY8 1
2006 ABC 0
2006 EFG 0
2006 HIJ 0
2007 XY8 1
2008 ABC 0
2008 EFG 0
2008 HIJ 0
;
run;
proc sql;
create table want as
select *, sum(cd='XY8')>0 as flg
from a
group by subjid;
quit;
Here is an SQL approach
data a;
input subjid cd$ d;
datalines;
2006 XY8 1
2006 XY8 1
2006 ABC 0
2006 EFG 0
2006 HIJ 0
2007 XY8 1
2008 ABC 0
2008 EFG 0
2008 HIJ 0
;
run;
proc sql;
create table want as
select *, sum(cd='XY8')>0 as flg
from a
group by subjid;
quit;
Anytime 🙂
You may try
data want;
set a;
if cd='XY8' then sort=0 ;
else sort=1;
run;
proc sort data=want;
by subjid sort;
run;
data want2;
set want;
by subjid sort;
retain flag;
if first.subjid then flag=' ';
if cd='XY8' then flag='Y';
run;
Problems like yours have been asked and answered so many times, that i am wondering why you haven't found something useful by searching the community. The following code is untested:
EDIT: i am sure that there where no answers, when i started writing mine, i am also sure, that writing so few lines didn't took three mins.
data want;
set have;
by subjid;
length flg $ 1;
retain flg;
if first.subjid then flg = ' ';
if cd = 'XY8' then flg = 'Y';
run;
Alternatively in a single data step you can try below code
data want;
do until(last.subjid);
set a;
by subjid;
if cd='XY8' then flag='Y';
end;
do until(last.subjid);
set a;
by subjid;
output;
end;
run;
Alternatively
data want;
do _N_ = 1 by 1 until (last.subjid);
set a;
by subjid;
if cd='XY8' then flg=1;
end;
do _N_ = 1 to _N_;
set a;
output;
end;
run;
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.