I have a datetime variable that I am having trouble converting into SAS. my date time variable is in the format dd/mm/yy tttt. An example of this date time is 11/5/2021 1525 for November 5 at 15:25 military time. Does anyone know if there is a formatting way to input this information into sas?
Read it as a string (or two strings) and then covert them into date and time and/or datetime values.
Examples:
data test;
input string1 :$10. string2 :$4. @1 longstr $30. ;
date1=input(string1,mmddyy10.);
date2=input(scan(longstr,1,' '),mmddyy10.);
time1=input(string2,hhmmss4.);
time2=input(scan(longstr,2,' '),hhmmss4.);
dt1=dhms(date1,0,0,time1);
dt2=dhms(input(scan(longstr,1,' '),mmddyy10.),0,0,input(scan(longstr,2,' '),hhmmss4.));
format date: yymmdd10. time: tod5. dt: datetime19. ;
cards;
11/5/2021 1525
;
PS Displaying dates in MDY order is going to confuse 50% of your audience.
Read it as a string (or two strings) and then covert them into date and time and/or datetime values.
Examples:
data test;
input string1 :$10. string2 :$4. @1 longstr $30. ;
date1=input(string1,mmddyy10.);
date2=input(scan(longstr,1,' '),mmddyy10.);
time1=input(string2,hhmmss4.);
time2=input(scan(longstr,2,' '),hhmmss4.);
dt1=dhms(date1,0,0,time1);
dt2=dhms(input(scan(longstr,1,' '),mmddyy10.),0,0,input(scan(longstr,2,' '),hhmmss4.));
format date: yymmdd10. time: tod5. dt: datetime19. ;
cards;
11/5/2021 1525
;
PS Displaying dates in MDY order is going to confuse 50% of your audience.
worked great, thanks for the help
I am mapping a data set from one program to another. When exported from the first program, the dates are all formatted as mm/dd/yyyy. In order to import the data into the second program, the dates must be formatted as yyyy/mm/dd. I am currently able to set this format in SAS, but when I export the data to excel, the date formats change back to mm/dd/yyyy. Is there a way to keep them as yyyy/mm/dd when exported?
@pengaentu wrote:
I am mapping a data set from one program to another. When exported from the first program, the dates are all formatted as mm/dd/yyyy. In order to import the data into the second program, the dates must be formatted as yyyy/mm/dd. I am currently able to set this format in SAS, but when I export the data to excel, the date formats change back to mm/dd/yyyy. Is there a way to keep them as yyyy/mm/dd when exported?
If you want Excel to display dates in a particular style you need to tell Excel that fact.
How did you export the data to an Excel file?
What is the second program? Is it a SAS program? Why use an Excel file? Why not just write a CSV file instead?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.