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();
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();
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();
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();
Astounding, thanks for your help
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.
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?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.