/*Question_1 */
data k;
x=2;
if x=1 then y=100;
if x=2 then y=200;
if x=3 then y=300;
else y=27;
run;
/*Question_2 */
data _n;
set sashelp.class;
if sex='F';
if sex='M' ;
put name sex ;
run;
Hi Experts,
Question_1 :Why it gives x=2 and y=27 why not y=200 in k dataset could you please explain
Question_2: Why zero observations in _n dataset
Hi @pavank, regarding question 1: the else relates only to the "if" which is before it. Try this:
data k;
x=2;
if x=1 then y=100;
else
if x=2 then y=200;
else
if x=3 then y=300;
else y=27;
run;
Mayve it gives the results you want.
The comparison of this code and its results with the code and results of your question 1 should help to understand how nested if else works.
Best
Markus
Hi @pavank, regarding question 1: the else relates only to the "if" which is before it. Try this:
data k;
x=2;
if x=1 then y=100;
else
if x=2 then y=200;
else
if x=3 then y=300;
else y=27;
run;
Mayve it gives the results you want.
The comparison of this code and its results with the code and results of your question 1 should help to understand how nested if else works.
Best
Markus
Hi @MarkusWeick
Thank you for your solution
Hi @pavank, regarding the second question:
Please read https://communities.sas.com/t5/SAS-Programming/Difference-between-only-IF-with-and-IF-with-THEN-stat...
Your if statements select only the rowas with sex = "F" and (!) sex = "M".
"and" and not "or". But there are now rows with sex = "F" and (!) sex = "M".
Hope this answers your question.
Best
Markus
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.