<?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 to rename a group of variables utilize array and looping method in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946141#M370560</link>
    <description>&lt;P&gt;If you want to get cute you can generate the RENAMES macro variable directly from the HAVE dataset by using an ARRAY and various SAS functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if 0 then set have;
  array old col: ;
  call symputx('renames',' ');
  do i=1 to dim(old);
    call symputx('renames',catx(' ',symget('renames')
                          ,catx('=',nliteral(vname(old[i])),cats('COL',i))
    ));
  end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 03 Oct 2024 15:23:59 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-10-03T15:23:59Z</dc:date>
    <item>
      <title>How to rename a group of variables utilize array and looping method</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946112#M370555</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a dataset 'Have' and I would like to rename the variables that start with 'COL'&amp;nbsp; to COL1-COLx, where x is how many variables that were using name COL in data 'Have'.&amp;nbsp; In my sample data, I would like to change vars COL1 COL3 COL88 to COL1 COL2 COL3. The 'Want' outcome is the bottom one:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stataq_0-1727964273072.png" style="width: 262px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100852iEA93C8758861B80F/image-dimensions/262x118?v=v2" width="262" height="118" role="button" title="stataq_0-1727964273072.png" alt="stataq_0-1727964273072.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stataq_1-1727964480366.png" style="width: 262px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100853iC4F4CC5BBAA0D2EB/image-dimensions/262x128?v=v2" width="262" height="128" role="button" title="stataq_1-1727964480366.png" alt="stataq_1-1727964480366.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;What should I do? Thinking of getting all variable that start with COL into array and then do the loop for rename, but get lost during the process. Could anyone guide me on this? thanks.&lt;/P&gt;
&lt;PRE&gt;data have;
input group $  COL1 COL3 COL88 value $;
datalines;
g1 30 45 36 Y
g2 37 25 55 N
g2 76  88 99 N
g3 22 74 . Y
g3 84 . 25 Y
g4 15  88 99 N
g4 21	 78 65 Y
g4 94 . . N
g4 73 8 78 Y
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 14:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946112#M370555</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-10-03T14:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a group of variables utilize array and looping method</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946120#M370556</link>
      <description>&lt;P&gt;First define in quite a bit more detail what "rename the variables that start with 'COL' to COL1-COLx, where x is how many variables that were using name COL in data 'Have'. " means. Walk through example(s).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best is to also include an input data set and what the output data set is expected to look like for that input as I am not at all sure how you get to to your "want" result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: Renaming is not done on an observation by observation basis which is what an "array" would do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is the result of a Proc Transpose (which is a common source of Col1-ColN naming) it might be better to go back one step and see if an ID statement might not help rename the data as desired. Which would mean provide an example of data input to the Proc Transpose and the transpose code.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 14:36:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946120#M370556</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-03T14:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a group of variables utilize array and looping method</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946139#M370559</link>
      <description>&lt;P&gt;You cannot use an ARRAY to RENAME variables.&amp;nbsp; You could use arrays to COPY the values to NEW variables.&lt;/P&gt;
&lt;P&gt;If you know how many variables there are then the code to do that could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ncol=3;
data want;
  set have;
  array old col: ;
  array new [&amp;amp;ncol] ;
  do _n_=1 to dim(old);
    new[_n_]=old[_n_];
  end;
  drop col: ;
  rename new1-new&amp;amp;ncol=col1-col&amp;amp;ncol;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if you want to just RENAME the variables (which will take less time and resources) you will need to do some CODE GENERATION instead.&amp;nbsp; One way would be to use list of variable names to generate a series of OLD=NEW rename pairs that could be used with a RENAME statement or a RENAME= dataset option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One easy way is to make an empty dataset that only has the COL... variables so that you can use the VARNUM metadata item to generate the new names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  set have(obs=0);
  keep col: ;
run;

proc sql noprint;
select catx('=',nliteral(name),cats('COL',varnum))
  into :renames separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='STEP1'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can now use this to either make a NEW dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  rename &amp;amp;renames ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or to modify the old one&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets nolist lib=work;
  modify have;
    rename &amp;amp;renames;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 15:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946139#M370559</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-03T15:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a group of variables utilize array and looping method</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946141#M370560</link>
      <description>&lt;P&gt;If you want to get cute you can generate the RENAMES macro variable directly from the HAVE dataset by using an ARRAY and various SAS functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if 0 then set have;
  array old col: ;
  call symputx('renames',' ');
  do i=1 to dim(old);
    call symputx('renames',catx(' ',symget('renames')
                          ,catx('=',nliteral(vname(old[i])),cats('COL',i))
    ));
  end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Oct 2024 15:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946141#M370560</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-03T15:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a group of variables utilize array and looping method</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946173#M370566</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;input group $ COL1 COL3 COL88 value $;&lt;BR /&gt;datalines;&lt;BR /&gt;g1 30 45 36 Y&lt;BR /&gt;g2 37 25 55 N&lt;BR /&gt;g2 76 88 99 N&lt;BR /&gt;g3 22 74 . Y&lt;BR /&gt;g3 84 . 25 Y&lt;BR /&gt;g4 15 88 99 N&lt;BR /&gt;g4 21 78 65 Y&lt;BR /&gt;g4 94 . . N&lt;BR /&gt;g4 73 8 78 Y&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set test;&lt;BR /&gt;array new[*] COL:; /* This Array references the original columns */&lt;BR /&gt;array CL[3]; /* This Array will create the new columns needed&lt;BR /&gt;Use a different name from the original&lt;BR /&gt;You must put a number of array elements*/&lt;BR /&gt;do x = 1 to dim(new);&lt;BR /&gt;CL[x] = new[x];&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;keep group CL: value;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 16:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-group-of-variables-utilize-array-and-looping/m-p/946173#M370566</guid>
      <dc:creator>JOL</dc:creator>
      <dc:date>2024-10-03T16:43:51Z</dc:date>
    </item>
  </channel>
</rss>

