SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
smoore3790
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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.

smoore3790
Fluorite | Level 6

worked great, thanks for the help

pengaentu
Calcite | Level 5

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?

Tom
Super User Tom
Super User

@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?

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1255 views
  • 1 like
  • 3 in conversation