BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ccaudillo100
Obsidian | Level 7

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

 


 

View solution in original post

4 REPLIES 4
Reeza
Super User

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

 

 


 

ccaudillo100
Obsidian | Level 7

Thank you! 🙂 

ballardw
Super User

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

 

 

 

johnagalloway0
Fluorite | Level 6

OK - you probably don't need to do this in macro language at all - but assuming you do, the following addresses numerous issues:

 

johnagalloway0_0-1675098156608.pngjohnagalloway0_1-1675098215628.png

It is common to overthink use of macros (especially if you have just come across them) but I would achieve the same result using:

johnagalloway0_2-1675098854206.png

Good luck, Enjoy SAS.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 4 replies
  • 751 views
  • 4 likes
  • 4 in conversation