0

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;
Betsy B
  • 77
  • 2
  • 11
  • Where does the date come in besides the output data set name? If it's not used anywhere the results will be identical between the iterations. – Reeza Mar 27 '16 at 18:13
  • trades_01jan2008 trades_02jan2008 I'm trying to append daily files with names like these. The code I have appends them into a monthly file called trades_jan2008. But I need to do this for several years of data and I was trying to do it with a macro. Thank you. – Betsy B Mar 27 '16 at 19:10
  • Where in the data step does the month come into play? Highlight the specific line please. – Reeza Mar 27 '16 at 20:03
  • dt = input(scan(ds_name, 2, "_"), date9.); day = day(dt); month = month(dt); year = year(dt); I am not sure how to highlight in the code but month is in the 3rd line above. Thank you. – Betsy B Mar 27 '16 at 20:14
  • But you don't do anything with it....is the code above your full code? – Reeza Mar 27 '16 at 20:16

1 Answers1

1

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.

Reeza
  • 20,510
  • 4
  • 21
  • 38
  • Ok, thank you, let me try what you suggest. What I gave in the original question is all the code I have. What it does is, it creates a day, month and year variable in the data set (these were not in the dataset, but only in its name, such as trades_01jan2008). Then, it appends each daily file into a monthly file. Since I have to do this for many different months I was wondering if there is a macro I could add, without doing each month manually (which is also possible). Thanks again. – Betsy B Mar 27 '16 at 21:34
  • The set statement used a colon so it appends all datasets that start with the prefix. You may want to consider explaining what you have and what your trying to achieve. There are a lot of posts on here regarding combining datasets. Check communities.sas.com for further examples. – Reeza Mar 27 '16 at 21:48