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

Abridged sample of my data below:

 

ACCOUNT, DATE

12345, 9/25/20

74156, 9/26/20

57831, 9/26/20

47856, 9/27/20

45178, 9/27/20

23654, 9/28/20

87654, 9/29/20

56789, 9/29/20

54321, 9/30/20

 

I would like to extract from this (sample) dataset all entries that occur between 9/26/20 to 9/29/20.

 

However - I would like a separate output for each date. If I used PROC SQL for example this is what my query would look like:

 

PROC SQL;

create table Sep26 as select * from DATA where date GE 9/26/20 and date LT 9/27/20;

quit;

PROC SQL;

create table Sep27 as select * from DATA where date GE 9/27/20 and date LT 9/28/20;

quit;

PROC SQL;

create table Sep28 as select * from DATA where date GE 9/28/20 and date LT 9/29/20;

quit;

PROC SQL;

create table Sep29 as select * from DATA where date GE 9/29/20 and date LT 9/30/20;

quit;

 

I would thus like just one query that loops over date range 9/26/20 to 9/29/20 and produces individual output for each date. I would imagine I need a macro variable to accomplish this?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
infile cards dsd;
input ACCOUNT DATE :mmddyy10.;
format date mmddyy10.;
cards;
12345, 9/25/20
74156, 9/26/20
57831, 9/26/20
47856, 9/27/20
45178, 9/27/20
23654, 9/28/20
87654, 9/29/20
56789, 9/29/20
54321, 9/30/20
;

/*Define your logic in a macro definition*/
%macro t;
 /*Declare your interval boundaries*/
  %let start=%sysevalf('26sep2020'd);
  %let end=%sysevalf('29sep2020'd);
  %do i=&start %to &end;
    proc sql;
	 create table Mon_%sysfunc(putn(&i,date9.))	as
	 select *
	 from have
	 where date=&i;
	quit;
   %end;
%mend t;

%t /*call and execute the macro*/

View solution in original post

5 REPLIES 5
Reeza
Super User
This is rarely useful, so it's usually not recommended in SAS especially since BY group processing within data steps is very efficient.

If you really want a macro look into this or CALL EXECUTE().
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

novinosrin
Tourmaline | Level 20
data have;
infile cards dsd;
input ACCOUNT DATE :mmddyy10.;
format date mmddyy10.;
cards;
12345, 9/25/20
74156, 9/26/20
57831, 9/26/20
47856, 9/27/20
45178, 9/27/20
23654, 9/28/20
87654, 9/29/20
56789, 9/29/20
54321, 9/30/20
;

/*Define your logic in a macro definition*/
%macro t;
 /*Declare your interval boundaries*/
  %let start=%sysevalf('26sep2020'd);
  %let end=%sysevalf('29sep2020'd);
  %do i=&start %to &end;
    proc sql;
	 create table Mon_%sysfunc(putn(&i,date9.))	as
	 select *
	 from have
	 where date=&i;
	quit;
   %end;
%mend t;

%t /*call and execute the macro*/
wylie_ma
Fluorite | Level 6

Thank you

 

Exactly what I was after. Much appreciated.

art297
Opal | Level 21

If you're going to do what you asked, take a look at the hash method as shown in the following presentation: https://www.sas.com/content/dam/SAS/en_ca/User%20Group%20Presentations/TASS/Tabachneck-SASTips-2011....

 

HTH,

Art

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1663 views
  • 4 likes
  • 5 in conversation