I have a time time variable exported from excel, and saved as character in SAS.
Time_var
| 12/13/2018 9:59:37.595 AM |
| 12/13/2018 10:01:17.595 AM |
| 09/06/2018 9:58:21.513 PM |
..............
I can't use the input function to convert Time_var to a numeric variable since SAS doesn't recognize the format. Any suggestion how to convert this variable into a numeric variable?
data have;
input datetime $30.;
cards;
12/13/2018 9:59:37.595 AM
12/13/2018 10:01:17.595 AM
09/06/2018 9:58:21.513 PM
;
data want;
set have;
numeric_dt=input(datetime,anydtdtm21.);
format numeric_dt datetime20.;
run;
data have;
input datetime $30.;
cards;
12/13/2018 9:59:37.595 AM
12/13/2018 10:01:17.595 AM
09/06/2018 9:58:21.513 PM
;
data want;
set have;
numeric_dt=input(datetime,anydtdtm21.);
format numeric_dt datetime20.;
run;
What number do you want?
You should be able to use ANYDTDTM informat to convert it to a datetime value (number of seconds since 1960). Or the ANYDTDTE informat to convert it to a date value (number of days since 1960). You could even use the ANYDTTME informat to just pull out the time of day value (number of seconds since midnight). Then attach a display format you like.
Note you might have issues with values like the last one where the order of the month and day of month is not clear from the values. The ANYDT.... informats will use the order associated with your current LOCALE settings. If you want to force SAS to use a particular order then you might want to parse the string first. For example to just get the date value you could use:
us_date = input(scan(time_var,1,' '),mmddyy10.);
uk_date = input(scan(time_var,1,' '),ddmmyy10.);
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.