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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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