BookmarkSubscribeRSS Feed
Dg112
Calcite | Level 5
My Data has the following date values in character variable type
Date
2013/12
2014/01
2014/02
2014/02
2012/06

How should I convert the variable type to numeric?
1 REPLY 1
Kurt_Bremser
Super User

Since you cannot change the type of a variable, you have to create a new one, which is done by doing the "rename-convert-drop" dance:

data have;
input date $7.;
datalines;
2013/12
2014/01
2014/02
2014/02
2012/06
;

data want;
set have (rename=(date=_date));
format date yymms7.;
date = input(compress(_date,"/"),yymmn6.);
drop _date;
run;

(SAS will automatically assume the first day of the month for the raw date values)

Alternatively, you can add the day yourself and use a complete date informat:

data want;
set have (rename=(date=_date));
format date yymms7.;
date = input(_date!!"/01",yymmdd10.);
drop _date;
run;

Or you can use the same method when initially bringing the data into SAS, which depends on the type of your data source.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 415 views
  • 0 likes
  • 2 in conversation