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;
@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:
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 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:
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.
Thanks , that was what I needed.
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Ready to level-up your skills? Choose your own adventure.