BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
liyongkai800
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
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;
Tom
Super User Tom
Super User

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

 

liyongkai800
Obsidian | Level 7
Thanks for the details. Originally I used input(time_var,datetimew.) directly, not anydtdtm... I think that's why it fails to convert. This is the first time I see the anydtdtm informat. Thanks again!

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
  • 3 replies
  • 689 views
  • 2 likes
  • 3 in conversation