BookmarkSubscribeRSS Feed
jossig
Calcite | Level 5
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?
3 REPLIES 3
Peter_C
Rhodochrosite | Level 12
data reduced;
do _n_=1 by 1 until( last.subject) ;
set your.data ;
by subject ;
if _n_ LE 4 then output ;
end;
run;
Vasile01
Fluorite | Level 6
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
Ksharp
Super User
[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
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1815 views
  • 0 likes
  • 4 in conversation