BookmarkSubscribeRSS Feed
ALF
Calcite | Level 5 ALF
Calcite | Level 5
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
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
data_null__
Jade | Level 19
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]
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
ALF
Calcite | Level 5 ALF
Calcite | Level 5
Thanks SPR, this code does just what I need! ALF
PatrickG
SAS Employee
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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1235 views
  • 0 likes
  • 5 in conversation