BookmarkSubscribeRSS Feed
rashmirao99
Fluorite | Level 6

Hi

I have a dataset where there are date and time variables. Date is in yyyy-mm-dd format and time is time5. format. Both are text varaibles.

I need to create a date time variable with iso8601 format. Some of the time values are missing.

I need a result with this condition:

if time is missing, then datetime variable should have only date part.

later I need to convert this datetime variable into Numeric value.

 

for example:

vsdat                    vstim

2018/01/19             

2017/12/22         10:53

 

thanks.

 

where vstim is blank.

3 REPLIES 3
novinosrin
Tourmaline | Level 20

@rashmirao99  If your vsdat variable and time vstim are  numeric, I'd suggest to keep the new variable as a datetime variable substituting 0 in place of missing and combine the two variables using dhms function. This will help in maintaining consistency in my opinion.  

Patrick
Opal | Level 21

One way to go:

data have;
  infile datalines dlm=' ' truncover;
  input vsdat $10. vstim $5.;
  datalines;
2018/01/19 
2017/12/22 10:53
;
run;

data want;
   set have;
   format dttm B8601DT.;
   dttm=input(catx(' ',vsdat,vstim),anydtdtm.);
run;
LinusH
Tourmaline | Level 20
You can't fulfil the requirement, at least not in SAS.
A numeric variable cannot contain mixed datetime and date values, this since formats can work only with either type. This is again feels like that someone who doesn't understand the implications for a requirement is set to specify one. And you with a quite distance from the end user just have to comply, no questions asked.
As suggested by others, set a valid time value when it's missing. Check with your stakeholders for guidance.
Or store the concatenated value in a string.
Data never sleeps

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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
  • 3 replies
  • 6490 views
  • 0 likes
  • 4 in conversation