BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Steelers_In_DC
Barite | Level 11

I have the following code that is working correctly:

%macro new_tran;

data new_tran;

set

%do i = 01 %to &num_month;

%let val=%sysfunc(putn(&i,z2.));

newtran.pid_defaults_final_&val.&year

%end;

;

run;

%mend;

%new_tran;

I would like to add something like this:

%if &num_month = 12 %then &year= &year - 1;

I tried to use %eval() but I couldn't get it to work.  &num_month = month(year()) -1.  I want to make sure that next January when the report runs for December that it takes the correct year.

I tried the following but it's really just a guess as I'm not that experienced with macros.  Any help will be appreciated:

%macro new_tran (num_month=12, year=2015);

%do i = 01 %to &num_month;

%let val=%sysfunc(putn(&i,z2.));

%let year_1 = %eval(&year -1);

%if num_month < 12 %then %do;

%put pid_defaults_final_&val.&year;

%end;

%if num_month = 12 %then %do;

%put pid_defaults_final_&val.&year_1;

%end;

%end;

%mend;

%new_tran;

Thanks

Mark

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Make sure to reference your variables properly:

%if num_month < 12 %then %do;

should be

%if &num_month < 12 %then %do;

BUT

you probably mean

%if &i< 12 %then %do;

and

%if &i= 12 then do;

As you do not change the value for &nummonth.

If this were my project I would consider passing a start and end date and then parse over those using the date incrementing function INTNX and put using a selected format to build the names you're using. Then I would not have a problem with any period of reports (providing the data of the appropriate month year combinations exist.

View solution in original post

2 REPLIES 2
ballardw
Super User

Make sure to reference your variables properly:

%if num_month < 12 %then %do;

should be

%if &num_month < 12 %then %do;

BUT

you probably mean

%if &i< 12 %then %do;

and

%if &i= 12 then do;

As you do not change the value for &nummonth.

If this were my project I would consider passing a start and end date and then parse over those using the date incrementing function INTNX and put using a selected format to build the names you're using. Then I would not have a problem with any period of reports (providing the data of the appropriate month year combinations exist.

Steelers_In_DC
Barite | Level 11

Makes perfect sense.  Just tested it, thanks for the help.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 662 views
  • 0 likes
  • 2 in conversation