I have a dataset that has a large number of variables (2,880 to be exact). Right now, the variables have the default variable names var1, var2, var3, ..., var2880. I need to label the variables according to 30 second intervals of military time - 00:00:00, 00:00:30, 00:01:00, 00:01:30, 00:02:00,...,23:59:30. I'm trying to figure out a way to do this without having to type all 2,880 by hand. I was thinking of using an array somehow, but I don't have a list of the desired labels stored anywhere, so I would still have to hand type them into the array. Any suggestions are appreciated!
here's my code to do this for 3 variables, obviously its easy to change for 2880 variables
/* I created a data set for me to work with, you don't have to do this step */
data have;
var1=0;
var2=0;
var3=0;
run;
%macro dothis;
proc datasets library=work /* or you can use another library */ nolist;
modify have; /* Your actual data set name goes here */
%do i=1 %to 3;
%let seconds=%eval((&i-1)*30);
%let thistime=%sysfunc(putn(&seconds,time8.));
/* %put &=i &=seconds &=thistime;*/
label var&i = "&thistime";
%end;
run; quit;
%mend;
%dothis
here's my code to do this for 3 variables, obviously its easy to change for 2880 variables
/* I created a data set for me to work with, you don't have to do this step */
data have;
var1=0;
var2=0;
var3=0;
run;
%macro dothis;
proc datasets library=work /* or you can use another library */ nolist;
modify have; /* Your actual data set name goes here */
%do i=1 %to 3;
%let seconds=%eval((&i-1)*30);
%let thistime=%sysfunc(putn(&seconds,time8.));
/* %put &=i &=seconds &=thistime;*/
label var&i = "&thistime";
%end;
run; quit;
%mend;
%dothis
I have a very hard time visualizing where that many variables would be desirable. Without very important reasons I would likely Transpose the data to one record per time period and then Create a time variable.
Something along these lines:
data want; set have; array v var1 - var2880 ; count=1; do time= "00:00:00"t to "23:59:30"t by 30; value = v[count]; output; count=count+1; end; keep /* other needed variables go here*/ time value; format time time8.; run;
Or even read the source date differently.
A good suggestion for most situations.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.