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-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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