Hi,
So, with the obs= option, like following:
data want;
set have(obs=10);
run;
It will limit the output dataset to have 10 observations. What I am asking is, how do you limit the dataset WANT itself? If I am creating a datset WANT based on the dataset HAVE, but there's an IF condition, and at the end I only want 100 observations even more 100 observations met the condition. I can't use the method above because I don't know how many observations it will take to meet the 100 observation conditions.
Example:
data want(obs=100);
set have;
if condition here;
run;
The above is just pseudo code.
Unfortunately the OBS= data set option is an INPUT only option.
You can achieve what you want in a data step by providing your own counter and conditionally writing to the output set:
data want ; set sashelp.class; retain counter; if sex='F' then do; counter+1; if counter le 3 then output; end; run;
This conditionally updates a counter variable and only writes to the output data set while the counter is less than or equal to 3. If the counter never reaches your limit all the cases where the condition is true will be written the output set.
Unfortunately the OBS= data set option is an INPUT only option.
You can achieve what you want in a data step by providing your own counter and conditionally writing to the output set:
data want ; set sashelp.class; retain counter; if sex='F' then do; counter+1; if counter le 3 then output; end; run;
This conditionally updates a counter variable and only writes to the output data set while the counter is less than or equal to 3. If the counter never reaches your limit all the cases where the condition is true will be written the output set.
I agree with the accepted answer, as you mention IF in the question.
But if the variables you want to use to subset the data exist in the dataset you are reading in, then you can use the WHERE option to subset your data and also use the OBS option. There WHERE will be applied first:
data want;
set sashelp.class(where=(sex='F') obs=3);
run;
You can also use the system option OBS to limit processing, just be very careful with it.
option obs=5;
data want;
set sashelp.class;
where sex='F';
run;
option obs=max;
data test;
set sashelp.class;
where sex eq "F"; /* apply filter condition */
if _n_ <4; /* limit the observation number * /
run;
The _n_ is based on the INPUT dataset and not the OUTPUT dataset right?
@cosmid wrote:
The _n_ is based on the INPUT dataset and not the OUTPUT dataset right?
_N_ is based on the number of iterations of the data step. In a simple single set statement data step it behaves like a row or observation counter.
The Set statement may be used multiple times, inside loops, as the result of conditional statements. Those can have a serious affect on how _n_ relates to numbers of observations.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.