BookmarkSubscribeRSS Feed
SAS_Ryan
Fluorite | Level 6

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;

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Reeza
Super User

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;


 

ballardw
Super User

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?

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 812 views
  • 2 likes
  • 4 in conversation