BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasuser123123
Quartz | Level 8

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

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.

Tom
Super User Tom
Super User

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
Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 556 views
  • 2 likes
  • 5 in conversation