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

 

Hi, 

I would like some help please

I have this peace of code which gives me a result of 354 for MONTHID1 ,  I don't know too much about macro so how do I get a new macro called month_id3  that results in 351 using MONTHID 1 , I have tried this 

 
%let today=%sysfunc(today());
 
mthid1=intck('month','01jan1990'd,&today.)+1-1;
 
call symput('MONTHID1',put(mthid1,3.));
 
%let month_id = %str(%')&MONTHID1.%str(%');/*'354';*/
 
%let month_id3 = '(&MONTHID1.-3)';   /* this doesn't work)
 
%put &month_id ***&month_id3***;
 
Many thanks
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In addition to @Kurt_Bremser excellent adivce, you generally do not want macro variables to have values that begins with a quote and ends with a quote, as in your example the macro variable has a value of '354'. This is really only making your life difficult, you cannot (easily) perform arithmetic on macro variables that have quotes to begin and end the value of the macro variable; and probably best to be avoided. For clarity, the code from Kurt Bremser produces macro variables whose value are 354 and 351, with no quotes, and the subtraction of 3 from 354 is simple.

--
Paige Miller

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

 

You cannot reference a macro variable in the usual way in a data step that is created by this data step.

"&" references are resolved at data step compile time, while call symput works at data step run time.

Simplify your code:

data _null_;
mthid1 = intck('month','01jan1990'd,today());
put mthid1=;
mthid3 = mthid1 - 3;
call symputx('mthid1',mthid1);
call symputx('mthid3',mthid3);
run;
%put mthid1=&mthid1;
%put mthid3=&mthid3;
jorquec
Quartz | Level 8

Many thanks. It was very helpful.

PaigeMiller
Diamond | Level 26

In addition to @Kurt_Bremser excellent adivce, you generally do not want macro variables to have values that begins with a quote and ends with a quote, as in your example the macro variable has a value of '354'. This is really only making your life difficult, you cannot (easily) perform arithmetic on macro variables that have quotes to begin and end the value of the macro variable; and probably best to be avoided. For clarity, the code from Kurt Bremser produces macro variables whose value are 354 and 351, with no quotes, and the subtraction of 3 from 354 is simple.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 845 views
  • 1 like
  • 3 in conversation