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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

8 REPLIES 8
cosmid
Lapis Lazuli | Level 10
oh, so the obs= count actually only increments if the condition is met?
data want;
set have(obs=10);
if condition here;
run;
The above code will only output 10 observations even if it reads in 1000 observations?
ballardw
Super User

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.

Quentin
Super User

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;
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
cosmid
Lapis Lazuli | Level 10
I forgot about the WHERE will be applied first. Thank you!
Reeza
Super User

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;
Ajayvit
Obsidian | Level 7

data test;
set sashelp.class;

where sex eq "F";    /* apply filter condition                */

if _n_ <4;                  /* limit the observation number * /
run;

cosmid
Lapis Lazuli | Level 10

The _n_ is based on the INPUT dataset and not the OUTPUT dataset right?

ballardw
Super User

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 6645 views
  • 5 likes
  • 5 in conversation