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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 934 views
  • 0 likes
  • 5 in conversation