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

When the date is imported it is formatted like this: 12/01/2023 (

SalesDateNum8MMDDYY10.

 

When I run this code, it deletes the dates:

 

/* Convert SalesDate from character to SAS date format */
data esales.import;
set esales.import;
/* Convert SalesDate to SAS date using MM/DD/YYYY format */
SalesDate = input(SalesDate, mmddyy10.);
format SalesDate date9.; /* Format for readability as DDMMMYYYY */
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@davidjayjackson wrote:

When the date is imported it is formatted like this: 12/01/2023 (

SalesDate Num 8 MMDDYY10.

 

When I run this code, it deletes the dates:

 

/* Convert SalesDate from character to SAS date format */
data esales.import;
set esales.import;
/* Convert SalesDate to SAS date using MM/DD/YYYY format */
SalesDate = input(SalesDate, mmddyy10.);
format SalesDate date9.; /* Format for readability as DDMMMYYYY */
run;


 

See comments on your code below. General idea:

  1. Do not use the same name in SET/DATA statements. This overwrites the data so it has to be recreated before it can be used again. 
  2. Dates in SAS are stored as numerics with a date format. To change the format, use a format statement

To fix your code first re-import your data, then try something like below. 

data esales.import_date_formatted;
set esales.import;
format SalesDate date9.; /* Format for readability as DDMMMYYYY */
run;

If you still have issues, please post your log and full code.

 

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

@davidjayjackson wrote:

When the date is imported it is formatted like this: 12/01/2023 (

SalesDate Num 8 MMDDYY10.

 

When I run this code, it deletes the dates:

 

/* Convert SalesDate from character to SAS date format */
data esales.import;
set esales.import;
/* Convert SalesDate to SAS date using MM/DD/YYYY format */
SalesDate = input(SalesDate, mmddyy10.);
format SalesDate date9.; /* Format for readability as DDMMMYYYY */
run;


 

See comments on your code below. General idea:

  1. Do not use the same name in SET/DATA statements. This overwrites the data so it has to be recreated before it can be used again. 
  2. Dates in SAS are stored as numerics with a date format. To change the format, use a format statement

To fix your code first re-import your data, then try something like below. 

data esales.import_date_formatted;
set esales.import;
format SalesDate date9.; /* Format for readability as DDMMMYYYY */
run;

If you still have issues, please post your log and full code.

 

 

 

davidjayjackson
Fluorite | Level 6

Thanks , that was what I needed.

Tom
Super User Tom
Super User

So SALESDATE is a numeric variable with DATE values. So there is no need to change the value to change how the value is displayed.  Just attach a different display format to the variable.

proc datasets nolist lib=esales ;
  modify import;
    format SalesDate date9.;
  run;
quit;

 

What is wrong with your program?  The INPUT() function needs a character string, not a number.  So SAS will first convert that date value into a character string using the BEST12. format.   Then the MMDDYY10. informat will try to read in the first 10 characters of that string and interpret that as if is a date value in MDY order (possibly with delimiters between the parts).

 

Let's try if on just a single date and see what happens.

476  data test;
477    startdate = '01MAY2024'd ;
478    string = put(startdate,best12.);
479    newdate = input(string,mmddyy10.);
480    put startdate=comma. startdate=mmddyy10. startdate=date9.
481      / string=$quote.
482      / newdate=comma. newdate=date9.
483    ;
484  run;

NOTE: Invalid argument to function INPUT at line 479 column 13.
startdate=23,497 startdate=05/01/2024 startdate=01MAY2024
string="       23497"
newdate=. newdate=.
startdate=23497 string=23497 newdate=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to
      missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 479:13
NOTE: The data set WORK.TEST has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.04 seconds

So the date 01MAY2024 is the number 23,497.  When converted to a string using the BEST12. format there will be 7 leading spaces, so the first 10 characters is just '       234' which does not look like a date in MDY style.

 

 

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

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1736 views
  • 6 likes
  • 3 in conversation