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

Hi All,

 

I need help with some code using either if, then, else, or an array.

 

I have a data set with many medication fields (wide data set). They are labeled medication1-medication29. I want to create a new variable called "code" that is equal to 1, if any of the 29 medications = 258. Then if medications =225 or 265 then code =2.

 

Here is some sample code I was playing with:

if (medication1 or medication2 or medication3 or medication4 or medication5...medication29)=258 then code=1;

else if (medication1 or medication2 or medication3 or medication4 or medication5...medication29)=225 or (medication1 or medication2 or medication3 or medication4 or medication5...medication29)=265 then code=2;

...

 

Thanks!

Sarah

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Look up the WHICHN or WHICHC functions depending on if your variable is character or numeric.

Assuming Numeric:

array med(29) medication1-medication29;

if whichn(258, of med(*))>0 then code=1;
else if (whichn(225, of med(*))>0 OR whichn(265, of med(*))>0) then code=2;

View solution in original post

2 REPLIES 2
Reeza
Super User

Look up the WHICHN or WHICHC functions depending on if your variable is character or numeric.

Assuming Numeric:

array med(29) medication1-medication29;

if whichn(258, of med(*))>0 then code=1;
else if (whichn(225, of med(*))>0 OR whichn(265, of med(*))>0) then code=2;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to add, with a do loop over your codes, you don't need all the conditional code either:

data have;
  infile datalines;
  input medication1-medication5;
datalines;
258 34 45 12 67
56 78 45 23 12
78 76 75 225 5
;
run;

data want;
  set have;
  array med{5} medication1-medication5;
  c=1;
  do i=258,225,657,234,123,897,445;
    if whichn(i,of med(*))>0 then code=c;
    c=c+1;
  end;
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
  • 2 replies
  • 908 views
  • 3 likes
  • 3 in conversation