BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tmcwill
Fluorite | Level 6

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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
--
Paige Miller
ballardw
Super User

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.

 

 

PaigeMiller
Diamond | Level 26

A good suggestion for most situations.

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 697 views
  • 1 like
  • 3 in conversation