Hello,
currently I pull in %let cdate=%sysfunc(putn("&sysdate9"d,YYMMDDD10.));
which displays 2023-01-30
what I would like to do is
%if mo(&cdate) = 01 %then %let Quarter = Q1;
%if mo(&cdate) = 04 %then %let Quarter = Q2;
%if mo(&cdate) = 07 %then %let Quarter = Q3;
%if mo(&cdate) = 10 %then %let Quarter = Q4;
I keep running into errors. Any help is much appreciated!
This can also refer to it as months of January, April, July, October, or abbreviations. I just need it to set Quarter based on Month
How about this instead:
%let Quarter= Q%sysfunc(putn("&sysdate9"d,QTR.));
@ccaudillo100 wrote:
Hello,
currently I pull in %let cdate=%sysfunc(putn("&sysdate9"d,YYMMDDD10.));
which displays 2023-01-30
what I would like to do is
%if mo(&cdate) = 01 %then %let Quarter = Q1;
%if mo(&cdate) = 04 %then %let Quarter = Q2;
%if mo(&cdate) = 07 %then %let Quarter = Q3;
%if mo(&cdate) = 10 %then %let Quarter = Q4;
I keep running into errors. Any help is much appreciated!
This can also refer to it as months of January, April, July, October, or abbreviations. I just need it to set Quarter based on Month
How about this instead:
%let Quarter= Q%sysfunc(putn("&sysdate9"d,QTR.));
@ccaudillo100 wrote:
Hello,
currently I pull in %let cdate=%sysfunc(putn("&sysdate9"d,YYMMDDD10.));
which displays 2023-01-30
what I would like to do is
%if mo(&cdate) = 01 %then %let Quarter = Q1;
%if mo(&cdate) = 04 %then %let Quarter = Q2;
%if mo(&cdate) = 07 %then %let Quarter = Q3;
%if mo(&cdate) = 10 %then %let Quarter = Q4;
I keep running into errors. Any help is much appreciated!
This can also refer to it as months of January, April, July, October, or abbreviations. I just need it to set Quarter based on Month
Thank you! 🙂
You are using 1) an invalid function "MO", the function would be month:
2) attempting to use a datastep fuction in macro code incorrectly: MUST use %sysfunc to call ANY data step fuction
3) attempting to use a date function on a string value (2023-01-30), is not a valid date value. SAS dates are numbers of days since 01Jan1960 and the Cdate value is not a number of days.
One way to get you quarter value
%let quarter= Q%sysfunc(qtr(%sysfunc(today()))); %put &quarter. ;
By using the letter Q immediately preceding the %sysfunc call then the function resolves the value is placed immediately after the Q So no %if %then %else code is required.
Note use of the QTR function. Your %if %then %else completely ignores what happens in other months and as long as your quarter actually aligns with calendar quarters it would be better to use the function.
For any date, time or datetime values that you expect to use other functions with or comparisons to actual date, time or datetime values you should not format the macro variable.
Unless you need to know the start of a SAS session instead of the current date you might consider using the TODAY function instead of that putn with "&sysdate9"d
OK - you probably don't need to do this in macro language at all - but assuming you do, the following addresses numerous issues:
It is common to overthink use of macros (especially if you have just come across them) but I would achieve the same result using:
Good luck, Enjoy SAS.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.