BookmarkSubscribeRSS Feed
jhsaunde
Calcite | Level 5

Hello!

 

My dataframe consists of many rows and 25 columns sequentially labeled 'diagx1' to 'diagx25'. Each row represents an individual presenting with one or more health conditions (diagx). There are many different conditions (10,000+), though some are more common than others. Multiple individuals can have the same condition.

 

I wish to narrow my dataframe down to 20 conditions: if any individual has one of these conditions in 'diagx1' to 'diagx25', I want them to remain in my dataframe. I have considered using multiple if statements, though I was wondering if someone had a more elegant solution.

 

 For example, say the 10 conditions were: NN1, NN2, NN3, QQ1, QQ2, QQ3, OO1, OO2, OO3, V1, V2

 

data hosp_prac;

set hosp;

 if diagx1 = NN1 or diagx1 = NN2 or diagx1 = NN3...

    or diagx2 = NN1 or diagx2 = NN2 or diagx2 = NN3...

    or diagx3 = NN1 or diagx3 = NN2...

         ....then output;

             run;

 

Any help would be greatly appreciated! I hope its clear enough - this is my first time posting - I'm not even sure if this belongs in this location.

 

Thanks!

3 REPLIES 3
Tom
Super User Tom
Super User

You could use an array to make the coding easier.

 

data hosp_prac;
  set hosp;
  array dx diagx1 - diagx25 ;
  found=0;
  do i=1 to dim(dx) while (found=0) ;
    found = dx(i) in ('NN1','NN2', ...... ) ;
  end;
  if found ;
run;
rogerjdeangelis
Barite | Level 11
I like the previous post better, but this is interesting

* create one ob where diag1-diag30 = NN1-NN30
data have;
  array diags[30] $3 diag1-diag30 (%macro gg;%do i=1 %to 30; "NN&i" %end;%mend;%gg);
run;quit;

* check for NN;
data want;
  set have;
  if index(catx(' ',of diag:),'NN');
run;quit;

1 ob written

* if you want  the postion of NN5;
data want;
  set have;
  array diags[30] $3 diag1-diag30 ;
  position = whichc('NN5',of diags[*]);
  put position;
run;quit;

5

rogerjdeangelis
Barite | Level 11
You can also do this

data want;
retain MaxPos;
set have;
array x[5];
MaxPos = whichn(max(of x[*]),of x[*]);
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 2304 views
  • 1 like
  • 3 in conversation