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

Probably an easy task for a pro but I got stuck with this trivial problem for quite some time now:

 

I have this table as a start:

 

data inTable;
input ICD $ Drugs MDD ;
datalines;
F111 . .
F60 . .
F132 . .
F323 . .
F331 . .

;

 I want to get the following results table:

data outtableDesired;
input ICD $ Drugs MDD ;
datalines;
F111 1.
F60 . .
F132 1 .
F323 . 1
F331 . 1

;

 

I tried the following:

data outtable;
set inTable;
if ICD in ('F11%','F13%') then do;
Drugs=1;
end;
if ICD in ('F32%','F33%') then do;
MDD=1;
end;
run;

 

Apparently '%' does not work in 'if ... then' procedures ... or? Any alternatives? Of course, my real table is much larger with many more options for ICD, that's why I have to write just the beginning of the character string and then '%'.

Thanks in advance for your time to help me with a solution! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:
Are you trying to use the % as a wildcard? The % and the _ work with the LIKE operator in a WHERE statement or clause. You cannot use them in an IF statement.
But, if you are searching for F11 or F13 in the beginning of a string (so your full value is something like F11AB or F1134 or F13BC, then using the =: logical operator can work in an IF statement.

See this example.

IF_not_LIKE.png

Cynthia

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

Shouldn't the syntax be

 

data outtable;
set inTable;
if ICD like ('F11%') or ICD like ('F13%') then do;
Drugs=1;
end;
run;

?

Cynthia_sas
SAS Super FREQ

Hi:
Are you trying to use the % as a wildcard? The % and the _ work with the LIKE operator in a WHERE statement or clause. You cannot use them in an IF statement.
But, if you are searching for F11 or F13 in the beginning of a string (so your full value is something like F11AB or F1134 or F13BC, then using the =: logical operator can work in an IF statement.

See this example.

IF_not_LIKE.png

Cynthia

haukewiegand
Fluorite | Level 6

Thanks, that's working well!

 

Hauke

Tom
Super User Tom
Super User

Note that the : modifier works with the IN operator also.

if code in: ('F11' 'F13') then .... ;
Cynthia_sas
SAS Super FREQ
Hi, Tom:
Good point!
Cynthia
haukewiegand
Fluorite | Level 6

Thanks, that really makes it easier!

 

Hauke

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 6 replies
  • 1441 views
  • 2 likes
  • 4 in conversation