Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Records

Status
Not open for further replies.

wundergal2000

Technical User
Jun 14, 2005
10
US
Hi,
How do I extract particular records in sas?For example, From the last observation, I want the preceeding 10 observations and no more.
For example:

Obs Name
1 a
2 e
3 l
4 gj
5 gd
6 td
7 sd
8 sg
9 s
10 sjkd
11 sjsk

I just want the observations from 2 through 11. Thanks so much for helping out a novice SAS user.
 
Hi,

You can use the _n_ variable to restrict the ouput.

data newdset;
set olddset;
if _n_ ge 2 and ne 11 then output newdset;
run;
 
You can also use the obs= and firstobs= options, for instance:-
Code:
data last10;
  set dset(obs=11 firstobs=2);
run;
Just remember that obs= doesn't specify how many record to read, it specifies the last record to read in.

If you don't know how many records are in the dataset when you are writing the code, then you can also use the following to get this:-
Code:
data _null_;
   set  dset nobs=count;
   call symput('obscount',trim(left(put(count,10.))));
   stop; 
run;

You can then put these together like this:-
Code:
data _null_;
   set dset nobs=count;
   call symput('obscount',trim(left(put(count,10.))));
   stcnt = count-9;
   call symput('stcount',trim(left(put(stcnt,10.))));
   stop; 
run;

data last10;
  set dset(obs=%eval(&obscount) firstobs=%eval(&stcount)) ;
run;

Enjoy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top