user3621066
user3621066

Reputation: 11

how to convert a string date variable into datetime variable in sas

I have a string variable for datetime. Sometimes it is a whole number like 3040000 sometimes a decimal value like this 3130215.123.

I would like to convert this into a date time variable like mm-dd-yyyy.

Thanks in advance.

add: I think the value 3130215.123 refers to feb-15-2013 12:30:00.

Upvotes: 0

Views: 1621

Answers (3)

mjsqu
mjsqu

Reputation: 5417

Based on comments on the original question, the statements would be:

format var_date ddmmyyd10. var_time time5. var_datetime datetime19.; 
var_date=input(put(int(17000000+(var)),10.),yymmdd10.);
var_time=input(put((var-int(var))*1e6,z6.),hhmmss6.);
var_datetime=dhms(var_date,hour(var_time),minute(var_time),0);

Haven't had a chance to test, so feel free to comment with any errors you get.

Upvotes: 0

Joe
Joe

Reputation: 63424

I think these are 3YYMMDD.HHMM

3130215.123 -> 3|13|02|15|12|30

(the last 0 is assumed).

So, you need:

length year month day hour minute second $2;
year=substr(dtvar,2,2);
month=substr(dtvar,4,2);
day=substr(dtvar,6,2);
hour=translate(subpad(dtvar,9,2),'0',' ');
minute=translate(subpad(dtvar,11,2),'0',' ');
second=translate(subpad(dtvar,13,2),'0',' ');

then

new_dtvar=dhms(mdy(month,day,year),hour,minute,second);

Upvotes: 1

DomPazz
DomPazz

Reputation: 12465

Dates, Times, and Datetimes are stored as doubles. Only the format matters for display.

try

var1 = input(var,best.); 

format var1 datetime19.;

in a data step to apply the format. Then look at the results.

Upvotes: 0

Related Questions