Discussion:
PROC SQL: Convert SAS Date to a string value
(too old to reply)
Mark Lurie
2013-08-05 17:52:20 UTC
Permalink
I have a date value in my dataset called calendar_month_end_date: 31MAR2011:00:00:00 that I want converted to YYYYMM and as a string so that I can concatenate with another string value to create a key.

I have tried a few different statements but nothing seems to work.

TIA
nina
2013-08-06 03:07:25 UTC
Permalink
Post by Mark Lurie
I have a date value in my dataset called calendar_month_end_date: 31MAR2011:00:00:00 that I want converted to YYYYMM and as a string so that I can concatenate with another string value to create a key.
I have tried a few different statements but nothing seems to work.
TIA
data have;
input d datetime20.;
datalines;
31MAR2011:00:00:00
run;
proc sql;create table want
as select substr(compress(put(datepart(d),yymmdd10.),"-"),1,6) as nd
from have;
quit;
data _null_;set;
put(_all_)(=);
run;
Reeza
2013-08-06 20:33:12 UTC
Permalink
Post by nina
Post by Mark Lurie
I have a date value in my dataset called calendar_month_end_date: 31MAR2011:00:00:00 that I want converted to YYYYMM and as a string so that I can concatenate with another string value to create a key.
I have tried a few different statements but nothing seems to work.
TIA
data have;
input d datetime20.;
datalines;
31MAR2011:00:00:00
run;
proc sql;create table want
as select substr(compress(put(datepart(d),yymmdd10.),"-"),1,6) as nd
from have;
quit;
data _null_;set;
put(_all_)(=);
run;
A bit more simply, using a year month format directly:

data have;
input d datetime20.;
datalines;
31MAR2011:00:00:00
run;

proc sql;
create table want
as select put(datepart(d),yymmn6.) as nd
from have;
quit;
p***@gmail.com
2015-02-13 02:10:38 UTC
Permalink
Post by nina
Post by nina
Post by Mark Lurie
I have a date value in my dataset called calendar_month_end_date: 31MAR2011:00:00:00 that I want converted to YYYYMM and as a string so that I can concatenate with another string value to create a key.
I have tried a few different statements but nothing seems to work.
TIA
data have;
input d datetime20.;
datalines;
31MAR2011:00:00:00
run;
proc sql;create table want
as select substr(compress(put(datepart(d),yymmdd10.),"-"),1,6) as nd
from have;
quit;
data _null_;set;
put(_all_)(=);
run;
data have;
input d datetime20.;
datalines;
31MAR2011:00:00:00
run;
proc sql;
create table want
as select put(datepart(d),yymmn6.) as nd
from have;
quit;
I know this thread is old, sorry,

Countless people online recommend the PUT method here, but SAS just won't let me do it. I have the same issue: I have a datetime object and I want a string that displays as YYYYMM. My datetime object is called DATE. So I am doing this:

PUT(datepart(DATE), yymmn6.)

But SAS refuses this, as follows:

ERROR: Character expression requires a character format.

Everyone suggests this solution, so obviously it works for people. Any idea why it won't work for me?

Regards
Glenn

Loading...