BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
niam
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

3 REPLIES 3
Ksharp
Super User

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.

Scott_Mitchell
Quartz | Level 8

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

Reeza
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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