<?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: Rename all variables in a dataset at once in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/778587#M247852</link>
    <description>&lt;P&gt;Hi! data_null has a good solution, but you can do it with less code. You can use the dictionary libname&amp;nbsp; or proc contents to get the list of variables from the dataset you want to change.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*option 1*/&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct compbl(name||" = "||compress(name||"_v1")) into :re separated by ' '&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname = 'WORK' and memname = 'MYDATA';&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*option 2*/&lt;/P&gt;&lt;P&gt;proc contents data = mydata out = var (keep = name) noprint;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct compbl(name||" = "||compress(name||"_v1")) into :re separated by ' '&lt;BR /&gt;from var;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*last step after option 1 or 2*/&lt;/P&gt;&lt;P&gt;proc datasets lib = work nolist;&lt;BR /&gt;modify mydata;&lt;BR /&gt;rename &amp;amp;re.;&lt;BR /&gt;run;quit;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Nov 2021 20:03:38 GMT</pubDate>
    <dc:creator>ehmsoleil</dc:creator>
    <dc:date>2021-11-04T20:03:38Z</dc:date>
    <item>
      <title>Rename all variables in a dataset at once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722608#M224084</link>
      <description>&lt;P&gt;Hello. I have a dataset with 10 variables (eg. client, price, volume, ship_date, purchase_date...). I would like to add v1 to all variables at once (eg. client_v1, price_v1, volume_v1, ship_date_v1, purchase_date_v1....). I am not sure how to do it. Thank you in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 16:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722608#M224084</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-03-01T16:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: Rename all variables in a dataset at once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722614#M224089</link>
      <description>&lt;OL&gt;
&lt;LI&gt;Generate fake data to work with (sample)&lt;/LI&gt;
&lt;LI&gt;Generate rename statement into a macro variable (look at WHERE for filter and CATT() for new variable name)&lt;/LI&gt;
&lt;LI&gt;Apply using PROC DATASETS, which doesn't recreate the data set, just modifies the names.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/********************************************************************
Example : Rename variables based on suffix rather than prefix
********************************************************************/

data sample;
do i=10000 to 12000;
	start_date=i;
	middle_date=i+3;
	end_date=i+5;
	date_no_change=start_date;
	output;
end;
format start_date end_date middle_date date9.;
run;


proc sql noprint;
select catx("=", name, catt('DT_', tranwrd(upper(name), '_DATE', ' '))) 
into :rename_list
separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='SAMPLE'
and upper(trim(name)) like '%_DATE';
quit;


%put &amp;amp;rename_list;


proc datasets library=work nodetails nolist;
modify sample;
rename &amp;amp;rename_list;
run; quit;

proc print data=sample noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A little more specifically for your use case, your SQL would look something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql noprint;
select catx("=", name, catt(name, "_v1")) 
into :rename_list
separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='SAMPLE';
quit;


%put &amp;amp;rename_list;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222217"&gt;@di_niu0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello. I have a dataset with 10 variables (eg. client, price, volume, ship_date, purchase_date...). I would like to add v1 to all variables at once (eg. client_v1, price_v1, volume_v1, ship_date_v1, purchase_date_v1....). I am not sure how to do it. Thank you in advance!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 16:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722614#M224089</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-01T16:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: Rename all variables in a dataset at once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722622#M224093</link>
      <description>&lt;P&gt;This macro will be helpful for this and other variable list manipulations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro
   expand_varlist /*Returns an expanded variable list and optionally creates an indexed data set of variable names*/
      (
         data  = _LAST_,            /*[R]Input data*/
         var   = _ALL_,             /*[R]Variable List expanded*/
         where = 1,                 /*[R]Where clause to subset OUT=, useful for selecting by a name suffix e.g. where=_name_ like '%_Status'*/
         expr  = nliteral(&amp;amp;name),   /*[R]An expression that can be used to modify the names in the expanded list*/
         keep  = ,                  /*[O]Keep data set option for DATA=*/
         drop  = ,                  /*[O]Drop data set option for DATA=*/
         out   = ,                  /*[O]Output data indexed by _NAME_ and _INDEX_*/
         name  = _NAME_,            /*[R]Name of the variable name variable in the output data set*/
         label = _LABEL_,           /*[R]Name of the variable name label variable in the output data set*/
         index = _INDEX_,           /*[R]Name of the variable index variable in the output data set*/
         dlm   = ' '                /*[R]List delimiter*/
      );
   %local m i;
   %let i=&amp;amp;sysindex;
   %let m=&amp;amp;sysmacroname._&amp;amp;i;
   %do %while(%symexist(&amp;amp;m));
      %let i = %eval(&amp;amp;i + 1);
      %let m=&amp;amp;sysmacroname._&amp;amp;i;
      %end;
   %put NOTE: &amp;amp;=m is a unique symbol name;
   %local rc &amp;amp;m code1 code2 code3 code4;
   %if %superq(out) ne %then %let code3 = %str(data &amp;amp;out(index=(&amp;amp;index &amp;amp;name)); set &amp;amp;out; &amp;amp;index+1; run;);
   %else %do;
      %let out=%str(work._deleteme_);
      %let code3 = %str(proc delete data=work._deleteme_; run;);
      %end;
   %let code1 = %str(options notes=0; proc transpose name=&amp;amp;name label=&amp;amp;label data=&amp;amp;data(obs=0 keep=&amp;amp;keep drop=&amp;amp;drop) out=&amp;amp;out(where=(&amp;amp;where)); var &amp;amp;var; run;);
   %let code2 = %str(proc sql noprint; select &amp;amp;expr into :&amp;amp;m separated by &amp;amp;dlm from &amp;amp;out; quit;);
   %let code4 = %str(options notes=1;);
   %let rc=%sysfunc(dosubl(&amp;amp;code1 &amp;amp;code2 &amp;amp;code3 &amp;amp;code4));
&amp;amp;&amp;amp;&amp;amp;m.
   %mend expand_varlist;

/*create sample data*/
data class;
   set sashelp.class;
   run;
/*Generate rename at once list*/
%let rename=%expand_varlist(data=class,expr=catx('=',_name_,cats(_name_,'_V1')));
%put NOTE: &amp;amp;=rename;

/*Rename*/
proc datasets;
   modify class;
   rename &amp;amp;rename; 
   run;
   contents data=class varnum;
   run;
   quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 839px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55319i31B95CE7C88BAF9C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 16:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722622#M224093</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2021-03-01T16:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Rename all variables in a dataset at once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722634#M224098</link>
      <description>Thank you very much!</description>
      <pubDate>Mon, 01 Mar 2021 17:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/722634#M224098</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-03-01T17:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Rename all variables in a dataset at once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/778587#M247852</link>
      <description>&lt;P&gt;Hi! data_null has a good solution, but you can do it with less code. You can use the dictionary libname&amp;nbsp; or proc contents to get the list of variables from the dataset you want to change.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*option 1*/&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct compbl(name||" = "||compress(name||"_v1")) into :re separated by ' '&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname = 'WORK' and memname = 'MYDATA';&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*option 2*/&lt;/P&gt;&lt;P&gt;proc contents data = mydata out = var (keep = name) noprint;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct compbl(name||" = "||compress(name||"_v1")) into :re separated by ' '&lt;BR /&gt;from var;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*last step after option 1 or 2*/&lt;/P&gt;&lt;P&gt;proc datasets lib = work nolist;&lt;BR /&gt;modify mydata;&lt;BR /&gt;rename &amp;amp;re.;&lt;BR /&gt;run;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 20:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-in-a-dataset-at-once/m-p/778587#M247852</guid>
      <dc:creator>ehmsoleil</dc:creator>
      <dc:date>2021-11-04T20:03:38Z</dc:date>
    </item>
  </channel>
</rss>

