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

How can I set the value of first_academic_period to character.  

 

%let last_academic_period='201910';

%macro setFirstAndLastPeriod();
%let testing = %substr(&last_academic_period, 5, 2);
%put (&testing);
%if %substr(&last_academic_period, 5, 2) = '10' or %substr(&last_academic_period, 5, 2) = '20'
%then %let first_academic_period = input(%eval(&last_academic_period - 490), 6.);
%else %if %substr(&last_academic_period, 5, 2) = '30' %then
%let first_academic_period = input(%eval(&last_academic_period - 420), 6.);
%put &first_academic_period;
%mend;
%setFirstAndLastPeriod();

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Macro variables are already character.  There is nothing you can do to make them character ... they already are.  You don't need quotes (and in fact shouldn't use quotes).

 

In some circumstances, there are tools that let you treat macro variables as numeric.

 

All of this also means that you can simplify what you are trying to do dramatically:

 

%let last_academic_period=201910;

%macro setFirstAndLastPeriod();
%let testing = %substr(&last_academic_period, 5, 2);
%put (&testing);
%if &testing = 10 or &testing = 20
%then %let first_academic_period = %eval(&last_academic_period - 490);
%else %if &testing = 30 %then 
%let first_academic_period = %eval(&last_academic_period - 420);
%put &first_academic_period;
%mend;
%setFirstAndLastPeriod();

View solution in original post

5 REPLIES 5
DavidPhillips2
Rhodochrosite | Level 12

How can I set the value of first_academic_period to character?

 

%let last_academic_period='201910';

%macro setFirstAndLastPeriod();
%let testing = %substr(&last_academic_period, 5, 2);
%put (&testing);
%if %substr(&last_academic_period, 5, 2) = '10' or %substr(&last_academic_period, 5, 2) = '20'
%then %let first_academic_period = input(%eval(&last_academic_period - 490), 6.);
%else %if %substr(&last_academic_period, 5, 2) = '30' %then
%let first_academic_period = input(%eval(&last_academic_period - 420), 6.);
%put &first_academic_period;
%mend;
%setFirstAndLastPeriod();

Astounding
PROC Star

Macro variables are already character.  There is nothing you can do to make them character ... they already are.  You don't need quotes (and in fact shouldn't use quotes).

 

In some circumstances, there are tools that let you treat macro variables as numeric.

 

All of this also means that you can simplify what you are trying to do dramatically:

 

%let last_academic_period=201910;

%macro setFirstAndLastPeriod();
%let testing = %substr(&last_academic_period, 5, 2);
%put (&testing);
%if &testing = 10 or &testing = 20
%then %let first_academic_period = %eval(&last_academic_period - 490);
%else %if &testing = 30 %then 
%let first_academic_period = %eval(&last_academic_period - 420);
%put &first_academic_period;
%mend;
%setFirstAndLastPeriod();
DavidPhillips2
Rhodochrosite | Level 12

, thanks for your help

FreelanceReinh
Jade | Level 19

Hi @DavidPhillips2,

 

First of all, please delete the second copy of your question (which you seem to have posted by mistake) to avoid confusion.

 

Remember that macro variables always contain text. So, in most cases there is no need for quotation marks. I think if you delete all quotation marks and the likewise unnecessary (and syntactically incorrect) calls to the INPUT function (i.e., simply write ...=%eval(...);) in your code, the result will be much closer to your expectation.

ballardw
Super User

It is generally a sub-optimal approach to macro coding to not pass values into a macro explicitly. Better would be

%macro setFirstAndLastPeriod(last_academic_period);

and call

 

%setFirstAndLastPeriod(201920);

If you want the First_academic_period value available outside of the macro you will need to declare it as %global or use a different approach making the macro more of macro function.

 

%then %let first_academic_period = input(%eval(&last_academic_period - 490), 6.);

Input is not a macro function so the above code is going to set the macro variable to the text "input(xxxxx, 6.)" for differing input values. Is that the intent?

 

If this is a date manipulation you might consider moving it to data step code.

What are the magic numbers 490 and 420 supposed to represent? And what is the expected output for 201910, 201920 and 201930?

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

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
  • 1261 views
  • 0 likes
  • 4 in conversation