DATA Step, Macro, Functions and more

Help with date=mm/dd/yy

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Help with date=mm/dd/yy

Hi,

I want to run this code but clearly I missed something.

Any help please?

Thank you.

HC

data a; set timehour;
if date='11/29/17'd ;run;


but this one works

data a; set timehour;
if date='29NOV17'd ;
if date='29NOV2017'd ;
run;

Accepted Solutions
Solution
‎12-09-2017 06:07 PM
Super User
Super User
Posts: 7,942

Re: Help with date=mm/dd/yy

[ Edited ]

If does not make any difference what format is attached to your variable. Date constants are always expressed using strings in a form that the DATE informat can recognize.

 

Dates are stored in SAS as the number of days since '01JAN1960'd.  There are many formats that work with date values so that you can print them in a form that is understandable to you.  If fact you can attach any numeric format to them.

 

565  data _null_;
566    date='29NOV2017'd ;
567    put (4*date) (date11. +1 mmddyy10. +1 yymmdd10. +1 comma7. ) ;
568  run;

29-NOV-2017 11/29/2017 2017-11-29  21,152

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: Help with date=mm/dd/yy

Doesn't look like you missed anything to me.  Date literals must use the DATE format.

Super Contributor
Posts: 503

Re: Help with date=mm/dd/yy

The format of the date is :MMDDYY8.

and here is the notice:

 

10581
10582 data a; set timehour;
10583 if date='11/29/17'd ;run;
-----------
77
ERROR: Invalid date/time/datetime constant '11/29/17'd.

ERROR 77-185: Invalid number conversion on '11/29/17'd.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.A may be incomplete. When this step was stopped there were 0 observations
and 10 variables.
WARNING: Data set WORK.A was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

Valued Guide
Posts: 575

Re: Help with date=mm/dd/yy

The format of your date constant is invalid - it has to be the day of the month, the three letter abbreviation of the month and the year (all in quotes) followed by a d. In your case that would be '29NOV2017'd

Solution
‎12-09-2017 06:07 PM
Super User
Super User
Posts: 7,942

Re: Help with date=mm/dd/yy

[ Edited ]

If does not make any difference what format is attached to your variable. Date constants are always expressed using strings in a form that the DATE informat can recognize.

 

Dates are stored in SAS as the number of days since '01JAN1960'd.  There are many formats that work with date values so that you can print them in a form that is understandable to you.  If fact you can attach any numeric format to them.

 

565  data _null_;
566    date='29NOV2017'd ;
567    put (4*date) (date11. +1 mmddyy10. +1 yymmdd10. +1 comma7. ) ;
568  run;

29-NOV-2017 11/29/2017 2017-11-29  21,152

 

Super User
Posts: 9,925

Re: Help with date=mm/dd/yy

SAS has only one syntax for date literals, the one that worked (either 2- or 4-digit years). If you want to compare your date with another date format, use either put() or input() functions:

if put(mydate,yymmddd10.) = '2017-12-08' then .....;
if mydate = input('2017-12-08',yymmdd10.) then ......;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,358

Re: Help with date=mm/dd/yy

Posted in reply to KurtBremser

However dates past year 9999 must use a 5 digit year in a literal and cannot exceed 31DEC20000. Smiley Wink

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 225 views
  • 0 likes
  • 5 in conversation