<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how change column name using macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824754#M325733</link>
    <description>&lt;P&gt;To use names like that you have to do two things. First set the VALIDVARNAME option to ANY.&amp;nbsp; Second use NAME LITERALS.&lt;/P&gt;
&lt;P&gt;I would suggest it is much better to just attach LABELs to the variables instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data column_names;
  length bucket hour minute time 8 name $32 label $256 ;
  format time tod8.;
  do hour=0 to 23;
   do minute=0 to 45 by 15;
     bucket+1;
     time=hms(hour,minute,0);
     name = cats('BUCKET',bucket);
     label=catx('-',put(time,tod8.),put(time+'00:14:59't,tod8.));
     output;
   end;
  end;
run;

proc sql noprint;
  select catx('=',name,nliteral(label))
       , catx('=',name,quote(trim(label)))
    into :rename separated by ' '
       , :label separated by ' '
  from column_names
  ;
quit;

data have;
 length bucket1-bucket96 8;
run;

proc datasets nolist lib=WORK ;
  modify HAVE ;
   * rename &amp;amp;rename ;
   label &amp;amp;label ;
  run;
quit;

proc print data=have;
run;
proc print data=have label;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1658430680429.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73584iC430351F3624E4B4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1658430680429.png" alt="Tom_0-1658430680429.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jul 2022 19:11:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-07-21T19:11:55Z</dc:date>
    <item>
      <title>how change column name using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824736#M325718</link>
      <description>&lt;P&gt;I have a table and I want to change the name of some columns using macro. The initial names are Bucket1-Bucket96 and I want to change them to time intervals. for example change Bucket 1 to 00:00:00-00:14:59, Bucket2 to&amp;nbsp;00:15:00-00:29:59 and so on. this is my code t1 is the current table I have&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate if you can help me&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data column_name;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data column_name;
	length myVar $ 1000;
	*myVar='';
    time='00:00:00't; 
	i=0;
	do hour = 0 to 23;
		hours=intnx('HOUR',time,hour,'b');
	  	do minutes = 0 to 45 by 15; 
			i=i+1;
	 		begin=intnx('MINUTE',hours,minutes,'b');
			begin_char=put(begin,tod8.);
	    	end=intnx('MINUTE',begin,14,'e');
			end_char=put( end,tod8.);
			myVar=catx(' ',myVar,' ',cats('BUCKET',i,'=',cats('"',catx('-',begin_char, end_char),'"n')));
		end;
	end;
/*  call symput('rename_list',myVar);*/
run;
%Put &amp;amp;myVar;

data t2;
  set work.t1;
  rename &amp;amp;myVar;
run; &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 17:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824736#M325718</guid>
      <dc:creator>saeidrasti</dc:creator>
      <dc:date>2022-07-21T17:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: how change column name using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824739#M325721</link>
      <description>&lt;P&gt;Those will not be valid variable names and I'd highly recommend against using those names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead you can create labels for your data set so the labels will show with the time intervals.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data column_name;
	length myVar $ 32000;
    time='00:00:00't; 
    i=0;
	do time='00:00:00't to '23:59:00't by 900; /* 900 seconds is 15 minutes and SAS stores time in seconds*/	
	i+1;
			begin_char=put(time,tod8.);
	    	end=intnx('MINUTE',time,14,'e');
			end_char=put( end,tod8.);
			myVar=catx(' ',myVar,' ',cats('BUCKET',i,'=',cats('"',catx('-',begin_char, end_char)), '"'));
		end;
  call symput('label_list',myVar);
run;

%put &amp;amp;label_list;

data have;
array buckets(96) bucket1-bucket96;
do i=1 to 96;
buckets(i) = rand('bernoulli', 0.4);
end;
run;

proc datasets library=work;
modify have;
label &amp;amp;label_list;
run;

proc contents data=have;run;

proc print data=have label;
var bucket1-bucket5 bucket90-bucket96;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really want those names, you can change the solution above to your original approach and change the LABEL to RENAME in proc datasets. I REALLY REALLY do not recommend this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data column_name;
	length myVar $ 32000;
    time='00:00:00't; 
    i=0;
	do time='00:00:00't to '23:59:00't by 900;	
	i+1;
			begin_char=put(time,tod8.);
	    	end=intnx('MINUTE',time,14,'e');
			end_char=put( end,tod8.);
			myVar=catx(' ',myVar,' ',cats('BUCKET',i,'=',cats('"',catx('-',begin_char, end_char)), '"n'));
		end;
  call symput('label_list',myVar);
run;

%put &amp;amp;label_list;

data have;
array buckets(96) bucket1-bucket96;
do i=1 to 96;
buckets(i) = rand('bernoulli', 0.4);
end;
run;

proc datasets library=work;
modify have;
rename &amp;amp;label_list;
run;

proc contents data=have;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jul 2022 19:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824739#M325721</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-21T19:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: how change column name using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824754#M325733</link>
      <description>&lt;P&gt;To use names like that you have to do two things. First set the VALIDVARNAME option to ANY.&amp;nbsp; Second use NAME LITERALS.&lt;/P&gt;
&lt;P&gt;I would suggest it is much better to just attach LABELs to the variables instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data column_names;
  length bucket hour minute time 8 name $32 label $256 ;
  format time tod8.;
  do hour=0 to 23;
   do minute=0 to 45 by 15;
     bucket+1;
     time=hms(hour,minute,0);
     name = cats('BUCKET',bucket);
     label=catx('-',put(time,tod8.),put(time+'00:14:59't,tod8.));
     output;
   end;
  end;
run;

proc sql noprint;
  select catx('=',name,nliteral(label))
       , catx('=',name,quote(trim(label)))
    into :rename separated by ' '
       , :label separated by ' '
  from column_names
  ;
quit;

data have;
 length bucket1-bucket96 8;
run;

proc datasets nolist lib=WORK ;
  modify HAVE ;
   * rename &amp;amp;rename ;
   label &amp;amp;label ;
  run;
quit;

proc print data=have;
run;
proc print data=have label;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1658430680429.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73584iC430351F3624E4B4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1658430680429.png" alt="Tom_0-1658430680429.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 19:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824754#M325733</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-21T19:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: how change column name using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824834#M325782</link>
      <description>&lt;P&gt;Depending on what you have to do with the data in subsequent steps, it could be a good idea to transpose the data into a longer format, so that you have one Bucket variable holding the values that you want as variable names now.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 08:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824834#M325782</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-22T08:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: how change column name using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824854#M325790</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Depending on what you have to do with the data in subsequent steps, it could be a good idea to transpose the data into a longer format, so that you have one Bucket variable holding the values that you want as variable names now.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is a great idea. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/430051"&gt;@saeidrasti&lt;/a&gt; would be wise to follow this suggestion, which eliminates this renaming issue, and has additional benefit in simpler programming in subsequent steps.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 10:34:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/824854#M325790</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-22T10:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: how change column name using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/825333#M325999</link>
      <description>Thank you so much Reeza, it worked.</description>
      <pubDate>Mon, 25 Jul 2022 18:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-change-column-name-using-macro/m-p/825333#M325999</guid>
      <dc:creator>saeidrasti</dc:creator>
      <dc:date>2022-07-25T18:01:51Z</dc:date>
    </item>
  </channel>
</rss>

