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

In SAS Enterprise Guide, I'm trying to create a calculated column that converts a date (in the DATETIME26.6 format) to a date format fiscal month.

 

If the day is the 1st through the 21st of the month, I want the fiscal month to be the first day of that month.

If the day is the 22nd through the 31st of the month, I want the fiscal month to be the first day of THE NEXT month

 

As an example, Jan 21st 2019 should return Jan 1st 2019

And Jan 22nd 2019 should return Feb 1st 2019

 

I've written the code and tested both the INTNX and comparison in pieces (and they've worked), but when I include it in a case statement, my returns are all coming out as '."

 

Or analogously, if there's a better way of doing this, I'm happy to adopt a new solution altogether!

 

 

CASE
	WHEN DAY(DATEPART(t3.CRE_DT))<22
		THEN 	
			INTNX(	'DAY', 
				t3.CRE_DT, 
				1-DAY(DATEPART(t3.CRE_DT))
			)
	WHEN DAY(DATEPART(t3.CRE_DT))>=22
		THEN
			INTNX(	'MONTH', 
				INTNX(	'DAY', DATEPART(t3.CRE_DT), 1-DAY(DATEPART(t3.CRE_DT)), 
				1
			)
END
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

No need for a CASE statement at all. Consider IFN function in below.

data example;
   do CRE_DT='01JAN2019:12:15:00'dt to '31Jan2019:12:15:00'dt by 3600;
      newdate = ifn(day(datepart(CRE_DT))<22,datepart(intnx('dtmonth',CRE_DT,0,'B')),datepart(intnx('dtmonth',CRE_DT,1,'B')),.);
      output;
   end;
   format CRE_DT datetime19.  newdate date9.;
run;

 

Since your t3.CRE_DT variable is a DATETIME the proper interval would be 'DTDAY' or 'DTMONTH'

View solution in original post

3 REPLIES 3
ballardw
Super User

No need for a CASE statement at all. Consider IFN function in below.

data example;
   do CRE_DT='01JAN2019:12:15:00'dt to '31Jan2019:12:15:00'dt by 3600;
      newdate = ifn(day(datepart(CRE_DT))<22,datepart(intnx('dtmonth',CRE_DT,0,'B')),datepart(intnx('dtmonth',CRE_DT,1,'B')),.);
      output;
   end;
   format CRE_DT datetime19.  newdate date9.;
run;

 

Since your t3.CRE_DT variable is a DATETIME the proper interval would be 'DTDAY' or 'DTMONTH'

TelecomSasUser
Calcite | Level 5

Thanks so much! This worked like a charm (and a much cleaner solution than my original attempt)

ballardw
Super User

@TelecomSasUser wrote:

Thanks so much! This worked like a charm (and a much cleaner solution than my original attempt)


I'm not generally a fan of the IFN and IFC functions as they can lead to some longish hard to read code (like this) but I have even more of an issue with CASE statements.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3 replies
  • 1666 views
  • 0 likes
  • 2 in conversation