BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Quartz | Level 8
/*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

1 ACCEPTED SOLUTION

Accepted Solutions
MarkusWeick
Barite | Level 11

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

Please help to keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles

View solution in original post

3 REPLIES 3
MarkusWeick
Barite | Level 11

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

Please help to keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
pavank
Quartz | Level 8

Hi @MarkusWeick 

Thank you for your solution 

MarkusWeick
Barite | Level 11

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

Please help to keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles

sas-innovate-white.png

Missed SAS Innovate in Orlando?

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.

 

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 976 views
  • 3 likes
  • 2 in conversation