nmoorenz
nmoorenz

Reputation: 77

How do I convert from date to character string in a sas macro

This is part of a larger query, used to create a table that contains observations within a month, and using that month as the table name. I'd like the table name to be a sensible looking date, not a numeric.

Why does this %sysfunc(putc()) function not create a character string?

%macro transLiteralDate2Char(dateval=);  
%put numerical = &dateval.;  
%put sasdatavalue = %sysfunc(putc(&dateval,MONYY5.));
%mend;
%transLiteralDate2Char(dateval=%sysfunc(mdy(3,1,2016)));

Upvotes: 1

Views: 2646

Answers (2)

John
John

Reputation: 540

A put command in SAS always returns a character string. The distinction between %sysfunc(putc()) and %sysfunc(putn()) is what type of format the second argument of the function is - putc understands character formats and putn understands numeric formats. In this case, because date formats are numeric, you want to use %sysfunc(putn()) to print your string. Like this:

%macro transLiteralDate2Char(dateval=);  
  %put sasdatavalue = %sysfunc(putn(&dateval,MONYY5.));
%mend;
%transLiteralDate2Char(dateval=%sysfunc(mdy(3,1,2016)));

Check out the examples here for more info.

Upvotes: 2

Reeza
Reeza

Reputation: 21294

You can also use the second parameter in %SYSFUNC()

%put %sysfunc(today(), date9.);

Upvotes: 1

Related Questions