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 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 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 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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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