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.
Thanks
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.
%let start_date=&sysdate;
%put &start_date;
data temp;
do i=1 to 6;
date=intnx('month', "&start_date"d, -1*i);
date_formatted=put(date, monyy7.);
output;
end;
run;
proc sql;
select date_formatted into :keep_date_var separated by " "
from temp;
quit;
%put &keep_date_var;
data want;
set have;
keep id &keep_date_var;
run;
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).
proc sql;
select name into :collist separated by ' '
from dictionary.columns
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.];
quit;
data want;
set have;
keep &colllist.; *and any other needed variables.;
run;
Great help.. thanks to alll
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.