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
is there any meaning difference between the two method?
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.
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.
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?
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.
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.