Hi !
I tried to do a SAS code with severals THEN and ELSE statement for one IF condition but i can't do it. I tried this code and too without the ";" in each THEN statement.
How i can i do it ?
Thank you very much
DATA TEST ;
INPUT TYPE V1 V2 V3 V4 AMOUNT ;
CARDS ;
A 12 54 65 87 64
B 85 995 454 78 1
C 6 8 55 89 12 84
D 87 54 8 98 3 14
;
RUN ;
DATA TEMP ;
SET TEST ;
IF TYPE IN ("A", "B", "B") THEN
NEW_VBLE1 = V1 ;
NEW_VBLE2 = V2 ;
NEW_VBLE3 = V3 ;
NEW_VBLE4 = V4 ;
ELSE
NEW_VBLE1 = V1*AMOUNT;
NEW_VBLE2 = V2*AMOUNT ;
NEW_VBLE3 = V3*AMOUNT ;
NEW_VBLE4 = V4*AMOUNT ;
RUN ;
Please don't code all in uppercase, its like your shouting the code at us.
data temp;
set test;
if type in ("A","B") then do;
new_vble1=v1;
new_vble2=v2;
new_vble3=v3;
new_vble4=v4;
end;
else do;
new_vble1=v1*amount;
...
end;
run;
Hi @luciacossaro,
The "do; ... end;" technique suggested by RW9 can also be found in the example section of the IF-THEN/ELSE statement documentation (fifth bullet point).
Your first data step will work as soon as you insert a $ sign after "TYPE".
And if you have more choices to make, use a select statement:
select (type);
when ('A','B') do;
/* code */
end;
when ('C','D') do;
/* more code */
end;
otherwise do;
/* still more code */
end;
end;
Thank you for your answer. Is it possible that the code has an error? Because it do not work correctly, i believe it not consider the line " if type in ("A","B") then do;". I get this result :
TYPE | V1 | V2 | V3 | V4 | AMOUNT | new_vble1 | new_vble2 | new_vble3 | new_vble4 |
A | 12 | 54 | 65 | 87 | 64 | 768 | 3456 | 4160 | 5568 |
B | 85 | 995 | 454 | 78 | 1 | 85 | 995 | 454 | 78 |
C | 6 | 8 | 55 | 89 | 12 | 72 | 96 | 660 | 1068 |
D | 87 | 54 | 8 | 98 | 3 | 261 | 162 | 24 | 294 |
I assume that is a reply to me? If so then your type variable does not look like the test data you have provided as:
DATA TEST ; INPUT TYPE $ V1 V2 V3 V4 AMOUNT ; CARDS ; A 12 54 65 87 64 B 85 995 454 78 1 C 6 8 55 89 12 84 D 87 54 8 98 3 14 ; RUN ; data temp; set test; if type in ("A","B") then do; new_vble1=v1; new_vble2=v2; new_vble3=v3; new_vble4=v4; end; else do; new_vble1=v1*amount; end; run;
Works fine. Its likely you have a space or something, try strip(type) in ...
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.