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

I'm relatively new to SAS and I'm trying to create a new field based on the value in a variable titled STARTDATE formattted as DATE YYYYMMDD10. I want to use this variable to calculate a new variable labeled "FISCALYR". I need only the YYYY for this new variable, but I want it to be based on a fiscal year that runs from 07/01 to 06/30.

 

As an example if the STARTDATE is 20100625 I want the FISCALFY to reflect 2010. If the STARTDATE is 20100703 I want the FISCALFY to reflect 2011. I was thinking I could use the INTCK or INTNX functions, but I'm not sure how to make this work.

 

Thanks for the feedback!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Did the code produce what you want then?

 


@runningjay wrote:

Yes - it is a numeric field with a date format.

 

Thanks


 

View solution in original post

8 REPLIES 8
Reeza
Super User

What type and format are the date variables?

 

Are they numeric with a format applied such as yymmdd10, or are they a character and appear as 2018/06/28?

ballardw
Super User

Before going any further please confirm that your Startdate variable is 1) numeric and 2) has an actual SAS Format of yymmdd10. If the SAS format is not a date format displaying the values the way you want then your value is basically just a number and the Date related functions and such will not work. Use proc contents or the SAS explorer to view column properties or such. I ask for clarification because there is no SAS supplied format yyyymmdd10. There is yymmdd10. however.

 

IF the value is a SAS date value then

Fiscalyear = year(datevariable) + (month(datevariable) > 6);

 

data example;
   input date mmddyy10.;
   format date yymmdd10.;
   Fiscalyear = year(date) + (month(date) > 6);
datalines;
01/01/2016
06/20/2016
07/01/2016
;
run;

If your variable is not a date then you likely should actually turn it into an actual SAS date value as that makes things much easier. Best is usually to do so while reading or importing data.

 

runningjay
Fluorite | Level 6

Thanks for the suggestion and guidance. Both of the responses I received worked to achieve what I wanted to do including your suggestion. In looking back at my question I should have been more clear in what I was trying to do, but even with the lack of clarity you provided excellent guidance that provided what I needed. Thanks!

runningjay
Fluorite | Level 6

I should have stated more clearly. The STARTDATE is a DATE field with a format of YYMMDD10., not YYYYMMDD10. as stated previously.

Reeza
Super User

Not being a pain, just a clarification, SAS only has two types, numeric and character. So its a numeric with a date format or a character. 

I'm going to assume it's a date though in this case. 

 

You can use the YEAR function to get the year component and the MONTH() function to get the month.

 

if month(date) <= 6 then fiscal_Year = catx('/', year(date)-1, year(date));
else fiscal_year = catx('/', year(date), year(date)+1);

 

 

runningjay
Fluorite | Level 6

Yes - it is a numeric field with a date format.

 

Thanks

Reeza
Super User

Did the code produce what you want then?

 


@runningjay wrote:

Yes - it is a numeric field with a date format.

 

Thanks


 

runningjay
Fluorite | Level 6

Final solution I used was as follows:

if month(date) <= 6 then fiscal_Year = catx('/', year(date)-1, year(date));
else fiscal_year = catx('/', year(date), year(date)+1);

This provided the FISCAL YEAR formatted as needed. Thanks for the feedback especially given the lack of clarity in my original question.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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