BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nietzsche
Lapis Lazuli | Level 10

Another question regarding to subsetting. 

 

what is the difference between

data usa1;
 set spg.usa;
 if _n_=5; output;
run;
proc print data=usa1;run;

and

data usa2;
 set spg.usa;
 if _n_=5 then output;
run;
proc print data=usa2;run;

both gives same print result

Nietzsche_0-1669900402881.png

 

is there any meaning difference between the two method?

 

 

 

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

In your case, no difference. 

 

In terms of data step processing there is a difference. In your first code snippet, the 

 

if _n_=5;

 

is a Subsetting If Statement Meaning that any code that is after that statement is executed only if _N_ = 5 (in this case just an Output Statement). 

 

In your second code snippet the 

 

if _n_=5 then output;

 

is a simple if-then statement. Statements following that line (in this case there are none) will still be executed. 

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

In your case, no difference. 

 

In terms of data step processing there is a difference. In your first code snippet, the 

 

if _n_=5;

 

is a Subsetting If Statement Meaning that any code that is after that statement is executed only if _N_ = 5 (in this case just an Output Statement). 

 

In your second code snippet the 

 

if _n_=5 then output;

 

is a simple if-then statement. Statements following that line (in this case there are none) will still be executed. 

Nietzsche
Lapis Lazuli | Level 10

so there is another way to filter data in the data step is WHERE statement.

 

so there are like three ways to do it in SAS?

 

IF VAR = X; OUTPUT
IF VAR = X THEN OUTPUT;
WHERE VAR = X; OUTPUT;

and they all give the same result. How is WHERE statement different from the other two?

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
Kurt_Bremser
Super User

WHERE is applied to the input dataset(s). It can only work with variables present there. Observations filtered by it never make it into the data step iteration.

The subsetting IF does not filter observations, it prevents part of the data step code from being executed (all observations make it into a data step iteration), and it works with all variables present in the PDV. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 737 views
  • 2 likes
  • 3 in conversation