DATA Step, Macro, Functions and more

Month flag variable using a macro

Reply
New Contributor ALF
New Contributor
Posts: 2

Month flag variable using a macro

I'm new to this forum. Can you advise how to create a month/year variable using a macro? I have individual monthly files that I will merge together to look at trends. However when I try the code below, I either get missing data or if I turn it into a character variable, the "&i." becomes the variable. I'd appreciate any guidance you can send my way. Thanks!

%macro date;
%do i = 4 %to 12;
data total_&i._2009;
set tier.total_&i._2009;
date = &i._2009;
%end;
run;
%mend date;
%date
Super Contributor
Super Contributor
Posts: 3,174

Re: Month flag variable using a macro

Reviewing your code, I believe that you are attempting to assign "date" as a SAS DATE (numeric type) variable, possibly? Although you have no FORMAT statement defined.

You may need to look at using the MDY function to assign your SAS DATE variable and also use a FORMAT statement to assign "date" to the desired output format display.

Scott Barry
SBBWorks, Inc. Message was edited by: sbb
Respected Advisor
Posts: 3,777

Re: Month flag variable using a macro

Perhaps you can read all the monthly files into one data set and create a month flag in one step.

[pre]
libname tier '.';
data tier.total_4_2009 tier.total_5_2009 tier.total_6_2009 tier.total_7_2009 tier.total_8_2009;
do j = 1 to 4;
output;
end;
run;


data total;
set tier.total_: indsname=indsname;
date = input(cats('01',substr(indsname,index(indsname,'_'))),mmddyy12.);
format date yymm.;
run;
proc print;
run;
[/pre]
Super Contributor
Super Contributor
Posts: 365

Re: Month flag variable using a macro

Hello ALF,

This is a possible solution:

%macro date;
%do i = 4 %to 12;
data total_&i._2009;
set tier.total_&i._2009;
date = MDY(&i.,01,2009);
format date date7.;
run;
%end;
%mend date;
%date

Sincerely,
SPR
New Contributor ALF
New Contributor
Posts: 2

Re: Month flag variable using a macro

Thanks SPR, this code does just what I need! ALF
SAS Employee
Posts: 58

Re: Month flag variable using a macro

For future reference, there's a lot of good info on using SAS dates here:

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002200738.htm
Ask a Question
Discussion stats
  • 5 replies
  • 181 views
  • 0 likes
  • 5 in conversation