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

Is there a system option or similar that could be used to generate an ERROR whenever SAS relies on the value of the YEARCUTOFF option in order to interpret an ambiguous years?

 

Below illustrates how SAS uses YEARCUTOFF to interpret a two-digit year or one-digit year.

 

347  options yearcutoff=1800 ;
348  data _null_ ;
349    x1='7JUN2018'd ;
350    x2='7JUN18'd ;
351    x3='7JUN8'd ;
352    put x1= x2= x3= ;
353    format x1 x2 x3 date9. ;
354  run;

x1=07JUN2018 x2=07JUN1818 x3=07JUN1808

I think I want something like YEARCUTOFF=ERROR, so that any time SAS tried to use the value of year cut off, it would return an error.  This would be an offensive programming option, to say "I don't want my program to tolerate years with less than four digits."

 

I vaguely remember for Y2K the company I was at had some add-on (?) that would generate a NOTE or WARNING in the log whenever code had a two-digit year.  But I don't think it was an official SAS product, and I don't remember how it worked.

 

If there's no good way to do this now, I'll submit it as a ballot item.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Quentin wrote:

Thanks @ballardw.  The concern is really the broad issue of how SAS handles a year value that has less than four digits.  So would apply to date literals, informats, functions that create date values from text, etc etc.  Generally I expect my code and data to have four-digit years.  If it encounters a year with less than four digits (in any context), I'd like it to be an error. 

 

Agree, I think setting yearcutoff to an unusual value is the best that could be done currently.  But that forces you to add code to check each date value, which as you say is a hassle, and feels dangerous because you're intentionally risking creating invalid data that could be treated as valid.  It's kind of like using -99 for missing values instead of special missing values.

 

Would be nice to have a way to say "Hey SAS, if you don't know what year a value represents, don't guess!"  (To be more fair: "don't interpret the year according to the 100 year range specified in YEARCUTOFF").


Sounds like a Ballot option might be in order to see if you can get enough support.

 

I already have enough date checking such as "are people visiting the clinic before they are born" in my projects the yearcutoff isn't a major concern.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

I'm not sure if this would help jog your memory, but you can certainly capture the YEARCUTOFF value using:

 

%let y = %sysfunc(getoption(yearcutoff));

 

Quentin
Super User

Yeah, but it's not that I want to capture the value of YEARCUTOFF.  I want a way to tell SAS that I don't want to use YEARCUTOFF to interpret ambiguous years. I want my session to expect four digit years from my code and data, and return an error if it finds a two digit year.  So this would be for strict error handling, along the lines of varinitchk=error, varlenchk=error, or my favorite undocumented dsoptions=note2err.

 

Over time I've become a fan of offensive programming that errs on the side of throwing too many errors rather handling errors.  This thought about YEARCUTOFF was prompted today because over on SAS-L it was pointed out that SAS date literals don't mind random text at the end, so SAS is happy to accept junk like:

 

1    data _null_ ;
2      x="07Jun1meh"d ;
3      put x date9.;
4    run ;

07JUN2001


Because it ignores the text at the end, and interprets the 1 as a one-digit year.

 

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

Is the concern here about date literals or Informat behavior? I suspect the data literal behavior is going to be somewhat different.

 

It is likely a PITA but you could do something like set the YEARCUTOFF to something extremely unlikely, such as 2200. Then any time dealing with the dates if the Year is > 2200 set an error message. Or perhaps a custom date range for "likely" date ranges.

 

options yearcutoff=2200;
proc format library=work;
value mydates
'01JAN1960'D - '31DEC2030'd = [mmddyy10.]
other = "_error_";
run;
data junk;
   x='01Jan01'd;
   format x mydates.;
run;

options yearcutoff=1926;

You could test the result of the format to actually throw an error in a data step if desired.

 

Quentin
Super User

Thanks @ballardw.  The concern is really the broad issue of how SAS handles a year value that has less than four digits.  So would apply to date literals, informats, functions that create date values from text, etc etc.  Generally I expect my code and data to have four-digit years.  If it encounters a year with less than four digits (in any context), I'd like it to be an error. 

 

Agree, I think setting yearcutoff to an unusual value is the best that could be done currently.  But that forces you to add code to check each date value, which as you say is a hassle, and feels dangerous because you're intentionally risking creating invalid data that could be treated as valid.  It's kind of like using -99 for missing values instead of special missing values.

 

Would be nice to have a way to say "Hey SAS, if you don't know what year a value represents, don't guess!"  (To be more fair: "don't interpret the year according to the 100 year range specified in YEARCUTOFF").

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

@Quentin wrote:

Thanks @ballardw.  The concern is really the broad issue of how SAS handles a year value that has less than four digits.  So would apply to date literals, informats, functions that create date values from text, etc etc.  Generally I expect my code and data to have four-digit years.  If it encounters a year with less than four digits (in any context), I'd like it to be an error. 

 

Agree, I think setting yearcutoff to an unusual value is the best that could be done currently.  But that forces you to add code to check each date value, which as you say is a hassle, and feels dangerous because you're intentionally risking creating invalid data that could be treated as valid.  It's kind of like using -99 for missing values instead of special missing values.

 

Would be nice to have a way to say "Hey SAS, if you don't know what year a value represents, don't guess!"  (To be more fair: "don't interpret the year according to the 100 year range specified in YEARCUTOFF").


Sounds like a Ballot option might be in order to see if you can get enough support.

 

I already have enough date checking such as "are people visiting the clinic before they are born" in my projects the yearcutoff isn't a major concern.

Quentin
Super User

I added the ballot item:

https://communities.sas.com/t5/SASware-Ballot-Ideas/New-option-YearCutoff-ERROR-to-generate-error-wh...

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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