Betsy B
Betsy B

Reputation: 77

How to add macro in SAS to the date variable to create a dataset for each month separately?

How can I add macro to the below code to do the same thing for each month of the year separately? For example, I'd need to create sourceg.trades_nov2008 as well. Thank you.

data sourceg.trades_dec2008(drop=dt);
set sourceh.trades_: indsname=ds_name open=defer;

dt = input(scan(ds_name, 2, "_"), date9.);
day = day(dt);
month = month(dt);
year = year(dt);

newtime=time/1000;
format newtime time12.3;
Hour=hour(newtime);
Minute=minute(newtime);
Second=second(newtime);
run;

Upvotes: 0

Views: 392

Answers (1)

Reeza
Reeza

Reputation: 21264

You can create a macro out of your code by adding a %macro monthly(date) and a %mend to your code. Then you can call it repeatedly using either manual call or call execute.

%macro monthly(date);

 data sourceg.trades_&date;
 *rest of sas code;

 run;

%mend;

%monthly(nov2008);
%monthly(jan2008);

Given your current explanation of the problem I'm not sure what to suggest beyond this. I'll leave call execute as an exercise to you, as it's well covered on here. Additionally, if you have a specific range of dates you could add a loop instead, but I don't know if that's your situation.

Upvotes: 1

Related Questions