BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dennis_oz
Quartz | Level 8

 

 

Hi All,

I am trying to just extract 10 observations for a dataset and chose to do it in the below method. 

But it does not seem to work . Can you help me with my logic below  

 

Also before using the do while  i tried using do until but the code would not process . i put put statements and looked in the log

but do until would not even go past 'point 1' ..  

 

 

thanks .

 

 

data test;
set sashelp.cars;
counter+1;
run;

 

 

data test_out;
set test;
put 'point1';

do while (counter =<10);
output;
put 'point2';
counter +1;
end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@dennis_oz:

You can do both WHILE or UNTIL, and the auto-dropped counter _ N_ (or _IORC_ if you wish) can be incorporated into the loop specifications:

data want ;                                                                                                                             
  do _n_ = 1 by 1 while (_n_ <= 10) ;                                                                                                   
* do _n_ = 1 by 1 until (_n_  = 10) ;                                                                                                   
    set sashelp.class ;                                                                                                                 
    output ;                                                                                                                            
  end ;                                                                                                                                 
  stop ;                                                                                                                                
run ;  

However, your requirement of using WHILE or UNTIL doesn't make much sense because if you use FROM-TO specification, WHILE is applied implicitly:

data want ;                                                                                                                             
  do _n_ = 1 to 10 ;                                                                                                                    
    set sashelp.class ;                                                                                                                 
    output ;                                                                                                                            
  end ;                                                                                                                                 
  stop ;                                                                                                                                
run ;  

I agree with other responders that for a thing like that, you only need the OBS=10 data set option. Or, if you need to select N consecutive records from anywhere in the file, you can combine FIRSTOBS= with OBS=. For example, to select 10 records starting from record 5:

data want ;                                                                                                                             
  set sashelp.class (firstobs=5 obs=14) ;                                                                                               
run ;                                         

Kind regards

Paul D.  

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

You simply want to extract the first 10 obs from a data set, correct?

dennis_oz
Quartz | Level 8
yes , if possible using do while
PeterClemmensen
Tourmaline | Level 20

You can use Do While like this

 

data want;
    set sashelp.cars;
    do while (c < 10);
        output;
        c+1;
    end;
run;

However, problems like this are easier solved with the automatic _N_ variable

 

data want;
    set sashelp.cars;
    if _N_ <= 10;
run;

 

Shmuel
Garnet | Level 18

You don't need a counter, sas has its own implicit counter _N_:

You simply do:

data want;
  set have(obs=10);
run;

or if you insist use do while then

data want;
  set have;
       do while _N_ le 10;
            output;
      end;
run;

or

data want;
   set have;
        counter +1;
        do while counter le 10;
             output;
       end;
run;

in your code you probably got 10 times same first observation.

Amir
PROC Star

Note that some responses output the first observation 10 times and some require brackets around conditions.

 

If you really want the first 10 observations output, as opposed to the first observation output 10 times, using a do-while or a do-until loop then the set statement should be in the loop so that a new observation is read from the input data set in each iteration of the loop.

 

As you might already be aware, the SAS data step automatically iterates over a data set in a set statement without the need for explicit looping code. In which case my preferred solution is the obs=10 data set option demonstrated by @Shmuel.

 

Otherwise, if you are learning about loops and how they can be used to read data sets then you could try something like:

 

/* do-until loop */
data want(drop = counter);
   do until(counter ge 10);
      set sashelp.class;
      output;
      counter + 1;
   end;

   /* prevent any more data being read */
   stop;
run;


/* do-while loop */
data want(drop = counter);
   counter + 1;

   do while(counter le 10);
      set sashelp.class;
      output;
      counter + 1;
   end;

   /* prevent infinite looping */
   stop;
run;

/* do-iterative loop */
data want(drop = counter);
   do counter = 1 to 10;
      set sashelp.class;
      output;
   end;

   /* prevent any more data being read */
   stop;
run;

 

Amir.

 

 

ballardw
Super User

If you want to use a specific set of sequential observations in almost any procedure there is no need to create an additional data set at all. Data set options FIRSTOBS and OBS will do it.

 

proc print data=sashelp.cars(firstobs=1 obs=10);
run;

proc print data=sashelp.cars(firstobs=5 obs=15);
run;
hashman
Ammonite | Level 13

@dennis_oz:

You can do both WHILE or UNTIL, and the auto-dropped counter _ N_ (or _IORC_ if you wish) can be incorporated into the loop specifications:

data want ;                                                                                                                             
  do _n_ = 1 by 1 while (_n_ <= 10) ;                                                                                                   
* do _n_ = 1 by 1 until (_n_  = 10) ;                                                                                                   
    set sashelp.class ;                                                                                                                 
    output ;                                                                                                                            
  end ;                                                                                                                                 
  stop ;                                                                                                                                
run ;  

However, your requirement of using WHILE or UNTIL doesn't make much sense because if you use FROM-TO specification, WHILE is applied implicitly:

data want ;                                                                                                                             
  do _n_ = 1 to 10 ;                                                                                                                    
    set sashelp.class ;                                                                                                                 
    output ;                                                                                                                            
  end ;                                                                                                                                 
  stop ;                                                                                                                                
run ;  

I agree with other responders that for a thing like that, you only need the OBS=10 data set option. Or, if you need to select N consecutive records from anywhere in the file, you can combine FIRSTOBS= with OBS=. For example, to select 10 records starting from record 5:

data want ;                                                                                                                             
  set sashelp.class (firstobs=5 obs=14) ;                                                                                               
run ;                                         

Kind regards

Paul D.  

SAS Innovate 2025: Register Now

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!

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
  • 7 replies
  • 1085 views
  • 3 likes
  • 6 in conversation