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!
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.
Cynthia
Shouldn't the syntax be
data outtable;
set inTable;
if ICD like ('F11%') or ICD like ('F13%') then do;
Drugs=1;
end;
run;
?
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.
Cynthia
Thanks, that's working well!
Hauke
Note that the : modifier works with the IN operator also.
if code in: ('F11' 'F13') then .... ;
Thanks, that really makes it easier!
Hauke
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!
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.