DATA Step, Macro, Functions and more

Slicing a SAS datset

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 88
Accepted Solution

Slicing a SAS datset

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

DateVar1Var2
2JUL201112
5MAY201134
6DEC201256
7JAN201378

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


Accepted Solutions
Solution
‎07-11-2013 11:53 PM
Super User
Posts: 10,020

Re: Slicing a SAS datset

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.

View solution in original post


All Replies
Solution
‎07-11-2013 11:53 PM
Super User
Posts: 10,020

Re: Slicing a SAS datset

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.

Super Contributor
Posts: 297

Re: Slicing a SAS datset

That is a fantastic way to create dynamically named datasets.  Thanks for posting it.  I am sure this will come in handy.

Super User
Posts: 19,772

Re: Slicing a SAS datset

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.)

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 233 views
  • 8 likes
  • 4 in conversation