There are many records and results for the months of October, November, and December. The only three sites in the dataset are the ones in the table above, and the same person can be in the set more than once for a different test.
In need to:
Remove the last 300 records from the Health Dept(the ones with the latest dates) from October, the last 300 records from November, and the last 300 from December
Remove the last 800 records from the Hospital Clinic(the ones with the latest dates) from October, the last 800 records from November, and the last 800 from December
Remove the last 600 records from the Doctor Office(the ones with the latest dates) from October, the last 600 records from November, and the last 600 from December
What code would I need to use to get SAS to do this? I would appreciate anyone who would take the time to help me out here. Thanks.
Message was edited by: State2000
if your data can be put in order by the date on which you base "latest", then something like this is worth trying[pre] proc sort data= your.data out= sorted ;
by that_date ;
data reduced ;
set sorted( firstobs= 301 ) ;
That process will jump over the first 300 records.
A similar process for each data set where you want to ignore the 800 oldest would use[pre] set sorted( firstobs= 801 ) ;[/pre]
You can do it, this way--create a variable month to have the values of month and then create a counter by month,sort the dataset by descending dates to have latest ones on top,then delete records where counter value is more than 300.Here is the sample code for that.
proc sort data=a; by month; run;/*Not required if month values are sorted, in my case I didnt use it*/
data b ;
if first.month then cnt=1;
proc sort data=b out=c; by descending date;run;
if cnt>300 then delete;
Try this it worked for me, hope it does for you as well.
Other replies demonstrate use of pre-sorted data combined with SAS DATA step processing. A BY statement in the DATA step facilitates using IF FIRST.varname processing, however what is not mentioned is the need to include your "site" variable in the SORT and DATA step BY statement, in order to reset the internal counter variable with each new site value.
So, in order to remove "last nnn records" you will need to sort the data by some "date-period" which can be derived using the INTNX function. This date-period variable must already be in your SAS file when it comes to performing the FIRST.varname processing.
So, here's your processing flow:
1) if needed, DATA step to assign a month-start date using INTNX.
2) SORT step to order data by "site" and "date-period" (descending).
3) DATA step to for FIRST. processing to remove unwanted observations, as determined for each unique "site".