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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 973 views
  • 0 likes
  • 4 in conversation