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.

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