Hi ,
I have data like
data new;
input subj visit test crit $10
cards;
1 visit1 RBC
1 visit1 WBC Y
1 visit1 HEMO
1 visit2 RBC
1 visit2 WBC
1 visit2 HEMO
1 visit3 RBC
1 visit3 WBC
1 visit3 HEMO
So,if crit has value Y then we need to pull out the corresponding test across all visits, output should like...
subj visit test crit
1 visit1 WBC Y
1 visit2 WBC
1 visit3 WBC
Could anyone help me out how to do this.
Thanks.
Just merge the data to the subset with CRIT='Y'.
data have;
input subj visit $ test $ crit $ ;
cards;
1 visit1 RBC ,
1 visit1 WBC Y
1 visit1 HEMO .
1 visit2 RBC .
1 visit2 WBC .
1 visit2 HEMO .
1 visit3 RBC .
1 visit3 WBC .
1 visit3 HEMO .
;
proc sort;
by subj test visit;
run;
data want;
merge have
have(keep=subj test crit rename=(crit=_crit) where=(_crit='Y'))
;
by subj test;
if _crit='Y';
run;
proc print;
run;
Results:
Obs subj visit test crit _crit 1 1 visit1 WBC Y Y 2 1 visit2 WBC Y 3 1 visit3 WBC Y
Please test your example data code before posting it, and fix the issues that arise.
Being able to write a simple data step is essential.
While fixing the issues with the data step, you may want to add another subj, or is there only one in the data you have?
At first glance this seems to be a nice task for double dow-loop.
Just merge the data to the subset with CRIT='Y'.
data have;
input subj visit $ test $ crit $ ;
cards;
1 visit1 RBC ,
1 visit1 WBC Y
1 visit1 HEMO .
1 visit2 RBC .
1 visit2 WBC .
1 visit2 HEMO .
1 visit3 RBC .
1 visit3 WBC .
1 visit3 HEMO .
;
proc sort;
by subj test visit;
run;
data want;
merge have
have(keep=subj test crit rename=(crit=_crit) where=(_crit='Y'))
;
by subj test;
if _crit='Y';
run;
proc print;
run;
Results:
Obs subj visit test crit _crit 1 1 visit1 WBC Y Y 2 1 visit2 WBC Y 3 1 visit3 WBC Y
data have; input subj visit $ test $ crit $ ; cards; 1 visit1 RBC , 1 visit1 WBC Y 1 visit1 HEMO . 1 visit2 RBC . 1 visit2 WBC . 1 visit2 HEMO . 1 visit3 RBC . 1 visit3 WBC . 1 visit3 HEMO . ; proc sql; create table want as select * from have where test in (select test from have where crit='Y'); quit;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.