12-23-2013 11:30 AM
Hi, I have a dataset with Dates as variables (Eg. Jan2008, Feb2008, Mar2008..... Oct2013, Nov2013)... how in SAS can I pick only the latest 6 months? so in this case only Jun2013 to Nov2013 .. I'm asking this becasue I want to automate it, and not change the months every month.
12-23-2013 12:11 PM
Here's a simple way, not the most efficient but easy to follow and easy to modify:
I create 1 macro variables that contain the names of the months of interest that can then be used in a keep statement in another data step. You can tweak the start date and the count intervals as needed.
do i=1 to 6;
date=intnx('month', "&start_date"d, -1*i);
select date_formatted into :keep_date_var separated by " "
keep id &keep_date_var;
12-23-2013 12:15 PM
I first off would suggest not having a dataset with months as variables. Store it in normalized form with months as rows. Then you can easily select the last 6 months' worth of data using a WHERE or subsetting IF statement, and if needed transpose back.
If you must keep it in horizontal format, you could select the variable names from dictionary.columns (or proc contents output) into a macro variable. Substitute proper names for LIBNAME and MEMNAME below (MEMNAME = dataset name).
select name into :collist separated by ' '
where libname='WORK' and memname='HAVE'
and input(name,MONYY.) ge intnx('Month','01NOV2013'd,-5); *you can easily adjust this to be programmatic, not sure what your conditions are for determining 'last month' - use INTNX to pick last 6 months once you have that defined [macro variable, last actual calendar month, etc.];
keep &colllist.; *and any other needed variables.;