<?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: Recoding variables from numbered list to month/year format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615006#M179843</link>
    <description>&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;Replace LIBRARY with the actual name of the library where your data set exists&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select cats(name,'=mba_',put(input(scan(label,-2,'/, '),2.),monname3.),'_',
		substr(scan(label,-1,'/, '),3,2))
		into :renames separated by ' ' from dictionary.columns
	    where libname='LIBRARY' and memname='YourDataSetName' and name eqt 'MBA_';
quit;
proc datasets library=LIBRARY nolist;
    modify YourDataSetName;
    rename &amp;amp;renames;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 Jan 2020 16:45:07 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-01-03T16:45:07Z</dc:date>
    <item>
      <title>Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/614999#M179839</link>
      <description>&lt;P&gt;I need to recode a long list of variables that are&amp;nbsp;named this way (numbered list by month, from first to last):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Variable&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Variable label&lt;/P&gt;&lt;P&gt;&lt;EM&gt;MBA_1_VALUE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Monthly Benefit Amount, 1/1962"&lt;/EM&gt;&lt;/P&gt;&lt;DIV&gt;&lt;EM&gt;MBA_2_VALUE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Monthly Benefit Amount, 2/1962"&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;....&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;MBA_672_VALUE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Monthly Benefits Amount, 12/2017"&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;To be coded this way&amp;nbsp;(month/abbr. year):&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;mba_jan_62&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Monthly Benefit Amount, 1/1962"&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;mba_feb_62&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Monthly Benefit Amount, 2/1962"&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;EM&gt;....&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;mba_dec_17&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Monthly Benefits Amount, 12/2017"&lt;/EM&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Is there&amp;nbsp;an efficient way to recode all these variables (via macros, arrays, proc sql, etc.)? There are 672 of these per variable type (I have 4 different variable types).&amp;nbsp;I don't need variable labels (used to help explain the format). All variables are character variables. Thank you!&lt;/DIV&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/614999#M179839</guid>
      <dc:creator>gurre003</dc:creator>
      <dc:date>2020-01-03T16:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615001#M179840</link>
      <description>&lt;P&gt;Well, the idea of having variable names with calendar information in the variable name is usually discouraged, as it is a poor practice that usually winds up making your coding more difficult. In addition, for creating tables and reports and such, the LABEL ought to be sufficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, I'm going to say YES there is an efficient way to rename all of these variables, but don't do it. You are making the rest of your coding more difficult.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615001#M179840</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-03T16:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615004#M179841</link>
      <description>&lt;P&gt;I'll clarify, this formatting is not something I want to do, but something I have to do in order to get other programs to run since those programs are set up to read month/year variable names. Either I do this manually (variable by variable)&amp;nbsp;or I create some efficient code to do this.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615004#M179841</guid>
      <dc:creator>gurre003</dc:creator>
      <dc:date>2020-01-03T16:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615005#M179842</link>
      <description>&lt;P&gt;I would suggest changing the plan to use names that look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;mba_1962_01&lt;/P&gt;
&lt;P&gt;mba_1962_02&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;mba_2017_12&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Those names are just as descriptive as what you are planning on now, but they have the advantages of using four-digit years and sorting in the proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that sounds like a good idea to you, I could propose a way to get there from where you are now.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615005#M179842</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-01-03T16:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615006#M179843</link>
      <description>&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;Replace LIBRARY with the actual name of the library where your data set exists&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select cats(name,'=mba_',put(input(scan(label,-2,'/, '),2.),monname3.),'_',
		substr(scan(label,-1,'/, '),3,2))
		into :renames separated by ' ' from dictionary.columns
	    where libname='LIBRARY' and memname='YourDataSetName' and name eqt 'MBA_';
quit;
proc datasets library=LIBRARY nolist;
    modify YourDataSetName;
    rename &amp;amp;renames;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615006#M179843</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-03T16:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615011#M179846</link>
      <description>&lt;P&gt;Can you rename ALL of them? If you are forced to use date strings in names it is better to use Y-M-D order so that they will sort alphabetically in chronological order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway here is an easy way to detect the date represented by either type of name and use that date to generate a new name.&lt;/P&gt;
&lt;P&gt;First let's make some test data.&amp;nbsp; In real life you would get the list from your dataset's metadata (via PROC CONTENTS or one of the DICTIONARY metadata tables/views).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input name :$32.;
cards;
MBA_1_VALUE          "Monthly Benefit Amount, 1/1962"
MBA_2_VALUE           "Monthly Benefit Amount, 2/1962"
MBA_672_VALUE       "Monthly Benefits Amount, 12/2017"
mba_jan_62           "Monthly Benefit Amount, 1/1962"
mba_feb_62          "Monthly Benefit Amount, 2/1962"
mba_dec_17          "Monthly Benefits Amount, 12/2017"
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's run a simple data step to generate the new name we want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  length date 8 new_name $32.;
  format date yymmdd10.;
  name=upcase(name);
  if scan(name,-1,'_')='VALUE' then
    date = intnx('month','01JAN1962'd,input(scan(name,-2,'_'),32.)-1,'b')
  ;
  else date=input(cats('01',scan(name,-2,'_'),scan(name,-1,'_')),date9.);
  new_name=catx('_',scan(name,1,'_'),put(date,yymm7.));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs        name               date     new_name

 1     MBA_1_VALUE      1962-01-01    MBA_1962M01
 2     MBA_2_VALUE      1962-02-01    MBA_1962M02
 3     MBA_672_VALUE    2017-12-01    MBA_2017M12
 4     MBA_JAN_62       1962-01-01    MBA_1962M01
 5     MBA_FEB_62       1962-02-01    MBA_1962M02
 6     MBA_DEC_17       2017-12-01    MBA_2017M12&lt;/PRE&gt;
&lt;P&gt;Now you can use those NAME/NEW_NAME pairs to generate OLDNAME=NEWNAME pairs into a macro variable.&amp;nbsp; You can then use the macro variable in a RENAME statement or RENAME= dataset option.&amp;nbsp; To change the names without copying the data use PROC DATASETS to modify the dataset and rename the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
 select catx('=',name,new_name) into :rename separated by ' '
 from have 
 where upcase(name) ne upcase(new_name)
 ;
quit;

proc datasets nolist lib=work;
  modify DS_TO_CHANGE;
  rename &amp;amp;rename ;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615011#M179846</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-03T16:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615012#M179847</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305435"&gt;@gurre003&lt;/a&gt;&amp;nbsp; &amp;nbsp;Here is a way to approach to RENAME reading dicitonary columns dynamically&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you have a dataset like the below sample named DSN, try to replicate the below to your needs and let us know&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Sample DSN*/
data dsn;
MBA_1_VALUE=25;
MBA_2_VALUE=35;
label MBA_1_VALUE='Monthly Benefit Amount, 1/1962'
MBA_2_VALUE='Monthly Benefit Amount, 2/1962';
run;
/*Generate dynamic RENAME statements using call execute from dictionary tables*/

data _null_;
set sashelp.vcolumn(keep=libname memname name label) end=z;
where libname='WORK' and memname='DSN';
w=put(input(scan(label,-1,','),ANYDTDTE10.),monyy7. -l);
want=catx('_',scan(name,1,'_'),w);
if _n_=1 then call execute('proc datasets lib=work nolist;modify dsn;rename ');
call execute(cats(' ',name,'=',want,' '));
if z;
call execute(cats(';','quit;'));
run;
/*Verify the change*/
proc contents data=dsn;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 16:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615012#M179847</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-03T16:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615016#M179849</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305435"&gt;@gurre003&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another (similar) approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename rencode temp;

data _null_;
file rencode;
put 'proc datasets lib=YOUR_LIBREF nolist;';
put 'modify YOUR_DATASET;';
put 'rename';
do i=1 to 672;
  d=intnx('month','01JAN1962'd,i-1);
  m=lowcase(put(d,monname3.));
  y=put(d,year2.);
  put 'MBA_' i +(-1) '_VALUE=mba_' m +(-1) '_' y;
end;
put '; quit;';
run;

%inc rencode;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just replace "YOUR_LIBREF" and "YOUR_DATASET" by the actual libref and dataset name, respectively.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 17:22:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615016#M179849</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-01-03T17:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding variables from numbered list to month/year format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615024#M179852</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305435"&gt;@gurre003&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know all too well how it is to deal with &lt;EM&gt;take-as-is-and-deliver-as-wanted &lt;/EM&gt;data. Here is another (tested) solution working after similar lines. It processes all numeric variables with &lt;U&gt;a given name pattern&lt;/U&gt;, and if you have more than one group of numeric variables, the&lt;EM&gt; if prxmatch ... end &lt;/EM&gt;section can be repeated and ajusted for each group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have one or more groups of character variables, you must declare a character variable array also and repeat the whole do loop to deal with the char array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc datasets changes names &lt;EM&gt;in situ&lt;/EM&gt;, so I strongly adwise to make a copy in work and then replace input when everything works OK.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Make test input data;
data have;
	attrib 
		ID length=8
		Department length=$30
		MBA_1_VALUE length=8 label="Monthly Benefit Amount, 1/1962"
		MBA_2_VALUE length=8 label="Monthly Benefit Amount, 2/1962"
		MBA_3_VALUE length=8 label="Monthly Benefit Amount, 3/1962"
	;
	ID = 1; Department = 'A'; MBA_1_VALUE = 1; MBA_2_VALUE = 2; MBA_3_VALUE = 3; output;
	ID = 2; Department = 'B'; MBA_1_VALUE = 11; MBA_2_VALUE = 22; MBA_3_VALUE = 33; output;
run;

* Rename variables in data set;
data _null_; set have (obs=1) end=end;
	array nums _numeric_;
	if _N_ = 1 then call execute('proc datasets nolist lib=work; modify have;');
	do i = 1 to dim(nums);
		vname = vname(nums{i});
		if prxmatch("/MBA_\d+_VALUE/",vname) then do;
			d = input(scan(vlabel(nums{i}),-1,' '),anydtdte.);
			ren = 'rename ' || trim(vname) || ' = mba_' || put(d,monname3.) || '_' || put(d,year2.)||';';
			call execute(ren);
		end;
	end;
	if end then call execute('run; quit;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 17:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recoding-variables-from-numbered-list-to-month-year-format/m-p/615024#M179852</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-03T17:59:30Z</dc:date>
    </item>
  </channel>
</rss>

