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

Hi,

 

Was wondering if there was a less tedious way to write my below code: Essentially I am trying to say that if the anti retroviral therapy they have is type 3, 4, 5, etc the they are using a combination ART (ie. 2). What I have below works, but is there an easier way to write this out?

 

DATA dtanl.hivcancerdta;
set dtanl.hivcancerdta;
IF arv1_d1 = . THEN arvtype = .;
ELSE IF arv1_d1 = 3 THEN arvtype = 2;
ELSE IF arv1_d1 = 4 THEN arvtype = 2;
ELSE IF arv1_d1 = 5 THEN arvtype = 2;
ELSE IF arv1_d1 = 11 THEN arvtype = 2;
ELSE IF arv1_d1 = 14 THEN arvtype = 2;
ELSE IF arv1_d1 = 42 THEN arvtype = 2;
ELSE IF arv1_d1 = 44 THEN arvtype = 2;
ELSE IF arv1_d1 = 45 THEN arvtype = 2;
RUN;

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
DATA dtanl.hivcancerdta;
set dtanl.hivcancerdta;
IF arv1_d1 = . THEN arvtype = .;
ELSE IF arv1_d1 in (3,4,5,11,14,42,44,45) then arvtype = 2;
RUN;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
DATA dtanl.hivcancerdta;
set dtanl.hivcancerdta;
IF arv1_d1 = . THEN arvtype = .;
ELSE IF arv1_d1 in (3,4,5,11,14,42,44,45) then arvtype = 2;
RUN;
--
Paige Miller
pramenon1
Obsidian | Level 7
Quick follow up, along with arv1_d1, i also have arv1_d2 and arv1_d3. Is there a way to make this same condition apply to arv1_d2 and arv1_d3 etc?

Below is the long way but is there a shorter way?

ELSE IF arv1_d1 in (3,4,5,11,14,42,44,45) then arvtype = 2;
ELSE IF arv1_d2 in (3,4,5,11,14,42,44,45) then arvtype = 2;
ELSE IF arv1_d3 in (3,4,5,11,14,42,44,45) then arvtype = 2;
ELSE IF arv2_d1 in (3,4,5,11,14,42,44,45) then arvtype = 2;
Tom
Super User Tom
Super User

To repeat the same logic across multiple variables use an array.

array list arv1_d1 arv1_d2 arv1_d3 arv2_d1 ;
arvtype=.;
do index=1 to dim(list) until (arvtype=2);
  IF list[index] in (3,4,5,11,14,42,44,45) then arvtype = 2;
end;
Tom
Super User Tom
Super User

Use the IN operator instead of the = operator.

For consecutive integers you can even use the min:max syntax. 

IF arv1_d1 = . THEN arvtype = .;
ELSE IF arv1_d1 in (3:5 11 14 42 44 45) THEN arvtype = 2;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 647 views
  • 6 likes
  • 3 in conversation