title: SAS macro - rename variables using their label values as their new variable names
body: <p>I want to produce a macro that converts the variable names of a dataset into the variables' labels. I intend to apply this macro for large datasets where manually changing the variable names would be impractical.</p>
  <p>I came across this <a href="http://support.sas.com/kb/24/720.html" rel="nofollow noreferrer">code online from the SAS website</a>, which looked promising but produced errors. I made slight edits to remove some errors. It now works for their sample dataset but not mine. Any assistance with improving this code to work with my sample dataset would be greatly appreciated!</p>
  <p>SAS sample dataset (works with code):</p>
  <pre><code>data t1;  
     label x='this_x' y='that_y';
     do x=1,2;
        do y=3,4;
           z=100;
           output;
        end;
     end;
  run;
  </code></pre>
  <p>My sample dataset (does not work with code):</p>
  <pre><code>data t1;
     input number group;
     label number = number_lab group = group_lab;
     datalines;
  1 1
  1 .
  2 1
  2 .
  3 2
  3 .
  4 1
  4 .
  5 2
  5 .
  6 1
  6 .
  ;
  run;
  </code></pre>
  <p>Code:</p>
  <pre><code>%macro chge(dsn);                                                                                                                       
     %let dsid=%sysfunc(open(&amp;dsn));                                                                                                        
     %let cnt=%sysfunc(attrn(&amp;dsid,nvars));                                                                                                 
     %do i= 1 %to &amp;cnt;                                                                                                                    
        %let var&amp;i=%sysfunc(varname(&amp;dsid,&amp;i));                                                                                             
        %let lab&amp;i=%sysfunc(varlabel(&amp;dsid,&amp;i));                                                                                            
        %if lab&amp;i = %then %let lab&amp;i=&amp;&amp;var&amp;i;                                                                                            
     %end;                                                                                                                                 
     %let rc=%sysfunc(close(&amp;dsid));                                                                                                        
                                                                                                                                          
     proc datasets;                                                                                                                          
        modify &amp;dsn;                                                                                                                           
        rename                                                                                                                                 
        %do j = 1 %to &amp;cnt;                                                                                                                  
           %if &amp;&amp;var&amp;j ne &amp;&amp;lab&amp;j %then %do;                                                                                                   
              &amp;&amp;var&amp;j=&amp;&amp;lab&amp;j                                                                                                                    
           %end;                                                                                                                               
        %end;                                                                                                                               
     quit;                                                                                                                                   
     run;                                                                                                                                    
                                                                                                                                          
  %mend chge;  
  
  %chge(t1)
  
  proc contents;                                                                                                                          
  run;
  </code></pre>
  <p>My code produces the following error messages:</p>
  <blockquote>
  <p>ERROR 73-322: Expecting an =.</p>
  <p>ERROR 76-322: Syntax error, statement will be ignored.</p>
  </blockquote>