Is there a way to programmatically loop thru all variables in a dataset and convert any date variables into character variables? I need to push my data to a 3rd party visualization software and it does not like the SAS formatted dates.
This code does the conversion I need, can it be macrotized to automatically convert for every date variable?
data my_dataset;
set my_dataset;
if my_date ne . then
my_date_ch = put(my_date, mmddyy10.);
else
my_date_ch = '';
run;
There's a simpler way than a loop.
Just assign the proper format to the date variables, and then when the data is exported somehow, the date variable appear in the selected format.
An array with a loop is likely all you need....if you have a naming convention this is much easier as well.
data want;
set have;
array _in(*) list of date variables here;
array _out(*) list of new variables here;
do i=1 to dim(_in);
_out = put(_in(i), ddmmyy10.);
end;
run;
@SAS_Ryan wrote:
Is there a way to programmatically loop thru all variables in a dataset and convert any date variables into character variables? I need to push my data to a 3rd party visualization software and it does not like the SAS formatted dates.
This code does the conversion I need, can it be macrotized to automatically convert for every date variable?
data my_dataset;
set my_dataset;
if my_date ne . then
my_date_ch = put(my_date, mmddyy10.);
else
my_date_ch = '';
run;
How many date variables are you talking about? It is likely easier to use a pair of arrays.
Something like:
data want; set my_dataset; array d my_date <list other date variables>; array c $ 10 my_date_ch <list the new names here>; do _i_ = 1 to dim(d); if d[i] then c[_i_] = put(d[_i_],mmddyy10.); end; run;
Make sure you have the same number of items in both arrays.
if you set
options missing=' ';
you wouldn't even need the test for if the existing date variable isn't missing.
Do they all have the same format?
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.
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.
Ready to level-up your skills? Choose your own adventure.