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.);
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!
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.