Hello;
I have create a pretty large dataset which contains over a million observations over 36 months between January 2010 and January 2013. thing like this
Date | Var1 | Var2 |
---|---|---|
2JUL2011 | 1 | 2 |
5MAY2011 | 3 | 4 |
6DEC2012 | 5 | 6 |
7JAN2013 | 7 | 8 |
Based on a timestamp variable (say DATE) I would like to create 36 separate datasets which each of them contains only the observations that fit within 1 month. So the first dataset contains the observations in January 2011, the second dataset contains the observations in February 2011, and so on.
Would you please let me know if there is an easy way to do this or I have to write a lot of IF statements?
Thanks
Hash Table:
data have; do date='01jan2010'd to '30jan2013'd ; monyy=put(date,monyy7.); var1=1;var2=1;output; output; end; run; proc sort data=have;by monyy;run; data want; if _n_ eq 1 then do; if 0 then set have; declare hash ha(multidata:'y'); ha.definekey('monyy'); ha.definedata('date','var1','var2'); ha.definedone(); end; do until(last.monyy); set have; by monyy; ha.add(); end; ha.output(dataset:monyy); ha.clear(); run;
Ksharp
Message was edited by: xia keshan Sorry I found problem in my code.
Hash Table:
data have; do date='01jan2010'd to '30jan2013'd ; monyy=put(date,monyy7.); var1=1;var2=1;output; output; end; run; proc sort data=have;by monyy;run; data want; if _n_ eq 1 then do; if 0 then set have; declare hash ha(multidata:'y'); ha.definekey('monyy'); ha.definedata('date','var1','var2'); ha.definedone(); end; do until(last.monyy); set have; by monyy; ha.add(); end; ha.output(dataset:monyy); ha.clear(); run;
Ksharp
Message was edited by: xia keshan Sorry I found problem in my code.
That is a fantastic way to create dynamically named datasets. Thanks for posting it. I am sure this will come in handy.
Generally not recommended.
http://www.sascommunity.org/wiki/Split_Data_into_Subsets
If for some reason you decide you really need to, you can create a month year variable from your date, using put(date, monyy7.)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.