Help using Base SAS procedures

Deleting observations

Reply
N/A
Posts: 1

Deleting observations

Hi,

I want to keep only the four first observations for each subject in my data set. I have data which I have arranged by subjects and date, i.e. constructed as follows:

Date Subject var
17837 1 1000
17867 1 1100
17898 1 1050
17929 1 1200
17957 1 1200
17988 1 1250
18018 1 1150
17837 2 1000
17867 2 1100
17898 2 1050
17929 2 1200
17957 2 1200
17988 2 1250
18018 2 1150

What I want to do is to make a series where I keep only the first four
observations for each subject, but drop the rest. Could someone please help
me with a SAS code for such procedure?
Valued Guide
Posts: 2,177

Re: Deleting observations

data reduced;
do _n_=1 by 1 until( last.subject) ;
set your.data ;
by subject ;
if _n_ LE 4 then output ;
end;
run;
Occasional Contributor
Posts: 14

Re: Deleting observations

Hi,


%let n=4;
data selected;
input Date Subject var;
cards;
17837 1 1000
17867 1 1100
17898 1 1050
17929 1 1200
17957 1 1200
17988 1 1250
18018 1 1150
17837 2 1000
17867 2 1100
17898 2 1050
17929 2 1200
17957 2 1200
17988 2 1250
1 3 1200
18018 2 1150
;


proc sort data=selected;
by subject date;


data selected;
set selected;
by subject date;
if first.subject then i=0;
i+1;
if i le &n then output;
drop i;
run;

A more complex example could be found at:
http://support.sas.com/kb/33/009.html

Warm regards,
Vasile
Super User
Posts: 10,023

Re: Deleting observations

[pre]
data temp;
input Date Subject var ;
cards;
17837 1 1000
17867 1 1100
17898 1 1050
17929 1 1200
17957 1 1200
17988 1 1250
18018 1 1150
17837 2 1000
17867 2 1100
17898 2 1050
17929 2 1200
17957 2 1200
17988 2 1250
18018 2 1150
;
run;
data result;
set temp;
if subject ne lag(subject) then count=0;
count+1;
if count le 4;
run;
[/pre]


Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 325 views
  • 0 likes
  • 4 in conversation