/*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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: