<?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: Change variable/column names to monthly increments in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584267#M166366</link>
    <description>&lt;P&gt;Another approach using PROC DATASETS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    array Coefficient_t{100} (100*0);
run;

data _null_;
    array Coefficient_t{100};
    length rname $5000;
    date='01May18'd;
    do i=1 to 100;
        date=intnx('month', date, 1, 'b');
        rname=catx(' ',rname, cats(vname(Coefficient_t[i]), '=', put(date, monyy7.)));
    end;
    retain rname;
    call symputx('rname', rname);
run;

%put &amp;amp;rname.;

proc datasets lib=work nolist;
    modify have;
        rename &amp;amp;rname.;
run;quit;

proc print data=have;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Aug 2019 15:01:48 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-08-27T15:01:48Z</dc:date>
    <item>
      <title>Change variable/column names to monthly increments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584259#M166361</link>
      <description>&lt;P&gt;Hi - first time poster here so will try my best to describe the problem I am facing. Also fairly newish to SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a number of variables all in one data set called:&lt;/P&gt;&lt;P&gt;Coefficient_t0 Coefficient_t1 Coefficient_t2&amp;nbsp; Coefficient_t3 etc up to a large number of coefficients (~100).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to somehow rename these to&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jun18 July18 August18 Spetember 18 etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was hoping to loop over the values of t0, t1, t2 and assign each =&amp;nbsp;intnx('month',Start_Date,i,"BEGINNING") this way each value of i should be a new month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let nvars=20;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro doit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Maturity_Coeffient_all_t_date;&lt;/P&gt;&lt;P&gt;set Maturity_Coeffient_all_t;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%do i = 1 %to &amp;amp;nvars.;&lt;/P&gt;&lt;P&gt;rename Coefficient_t&amp;amp;i. = intnx('month',Start_Date,i,"BEGINNING");&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%doit&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However this won't work...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any way to fix this?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 14:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584259#M166361</guid>
      <dc:creator>orange444</dc:creator>
      <dc:date>2019-08-27T14:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Change variable/column names to monthly increments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584264#M166363</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286839"&gt;@orange444&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi - first time poster here so will try my best to describe the problem I am facing. Also fairly newish to SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a number of variables all in one data set called:&lt;/P&gt;
&lt;P&gt;Coefficient_t0 Coefficient_t1 Coefficient_t2&amp;nbsp; Coefficient_t3 etc up to a large number of coefficients (~100).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to somehow rename these to&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jun18 July18 August18 Spetember 18 etc...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was hoping to loop over the values of t0, t1, t2 and assign each =&amp;nbsp;intnx('month',Start_Date,i,"BEGINNING") this way each value of i should be a new month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let nvars=20;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro doit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data Maturity_Coeffient_all_t_date;&lt;/P&gt;
&lt;P&gt;set Maturity_Coeffient_all_t;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i = 1 %to &amp;amp;nvars.;&lt;/P&gt;
&lt;P&gt;rename Coefficient_t&amp;amp;i. = intnx('month',Start_Date,i,"BEGINNING");&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;%doit&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However this won't work...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any way to fix this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Honestly, having variable names of your columns to be Jun18 July18&amp;nbsp;&lt;EM&gt;etc&lt;/EM&gt;. isn't really a good way to arrange data sets in SAS, although there are exceptions, such as if the data set has to be exported to Excel.&amp;nbsp;&lt;img id="smileymad" class="emoticon emoticon-smileymad" src="https://communities.sas.com/i/smilies/16x16_smiley-mad.png" alt="Smiley Mad" title="Smiley Mad" /&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;As with all macro writing, you want to first (in other words, before you write a single macro variable into your code) get&amp;nbsp;&lt;EM&gt;working valid legal&lt;/EM&gt; SAS code that does what you want. In this case you would see that&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename Coefficient_t01 = intnx('month',Start_Date,i,"BEGINNING");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(note there are no macro variables in the above code) does not work. You would need to fix this in order to have any chance of getting a macro to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Having said all of the above, your %DO loop should look like something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let start_date=%sysevalf('01JUL18'd); 
%do i=1 %to &amp;amp;nvars;
    %let y=%sysfunc(intnx(month,&amp;amp;Start_Date,&amp;amp;i));
    %let yf=%sysfunc(putn(&amp;amp;y,monyy5.));
    %put &amp;amp;=start_date &amp;amp;=y &amp;amp;=yf; /* This line is optional */
    rename coefficient_t&amp;amp;i = &amp;amp;yf;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 14:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584264#M166363</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-27T14:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: Change variable/column names to monthly increments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584266#M166365</link>
      <description>&lt;P&gt;One way to see what is happening is to turn on MPRINT option so you can see what SAS code your macro is generating.&amp;nbsp; Then you can adopt the macro to generate the SAS code you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now you macro is generating code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPRINT(DOIT):   rename Coefficient_t1 = intnx('month',Start_Date,i,"BEGINNING");
MPRINT(DOIT):   rename Coefficient_t2 = intnx('month',Start_Date,i,"BEGINNING");
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which is clearly not valid SAS syntax.&amp;nbsp; The RENAME statement wants a NAME after the equal sign and not function call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can call SAS functions in macro code by using the %SYSFUNC() macro function.&amp;nbsp; Put then functions arguments also need to make sense.&amp;nbsp; INTNX() function wants a date for the second argument, not a string of letters.&amp;nbsp; Similarly for the third argument.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also what value do you want to use for the name?&amp;nbsp; Currently you appear to be generating a date value.&amp;nbsp; So the number of days since 1960.&amp;nbsp; You probably want to apply a format to that date value so it generates something that is human readable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also there are only 12 months in a year.&amp;nbsp; So you cannot make 20 variables with names like the 18th of September, 18th of October etc.&amp;nbsp; Are you trying to imply you want the names to reflect just the month and year? If so then use four digits for the year or you will confuse people.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nvars=20;
%let start_date='01SEP2018'd;

%macro doit();
data Maturity_Coeffient_all_t_date;
  set Maturity_Coeffient_all_t;
  rename
%do i = 1 %to &amp;amp;nvars.;
   Coefficient_t&amp;amp;i. = %sysfunc(intnx(month,&amp;amp;Start_Date,&amp;amp;i-1,B),monyy7)
%end;
  ;
run;
%mend ;
options mprint ;
%doit()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;381   options mprint ;
382   %doit()
MPRINT(DOIT):   data Maturity_Coeffient_all_t_date;
MPRINT(DOIT):   set Maturity_Coeffient_all_t;
MPRINT(DOIT):   rename Coefficient_t1 = SEP2018 Coefficient_t2 = OCT2018 Coefficient_t3 = NOV2018
Coefficient_t4 = DEC2018 Coefficient_t5 = JAN2019 Coefficient_t6 = FEB2019 Coefficient_t7 = MAR2019
Coefficient_t8 = APR2019 Coefficient_t9 = MAY2019 Coefficient_t10 = JUN2019 Coefficient_t11 = JUL2019
Coefficient_t12 = AUG2019 Coefficient_t13 = SEP2019 Coefficient_t14 = OCT2019 Coefficient_t15 = NOV2019
Coefficient_t16 = DEC2019 Coefficient_t17 = JAN2020 Coefficient_t18 = FEB2020 Coefficient_t19 = MAR2020
Coefficient_t20 = APR2020 ;
MPRINT(DOIT):   run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 14:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584266#M166365</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-27T14:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: Change variable/column names to monthly increments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584267#M166366</link>
      <description>&lt;P&gt;Another approach using PROC DATASETS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    array Coefficient_t{100} (100*0);
run;

data _null_;
    array Coefficient_t{100};
    length rname $5000;
    date='01May18'd;
    do i=1 to 100;
        date=intnx('month', date, 1, 'b');
        rname=catx(' ',rname, cats(vname(Coefficient_t[i]), '=', put(date, monyy7.)));
    end;
    retain rname;
    call symputx('rname', rname);
run;

%put &amp;amp;rname.;

proc datasets lib=work nolist;
    modify have;
        rename &amp;amp;rname.;
run;quit;

proc print data=have;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 15:01:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-variable-column-names-to-monthly-increments/m-p/584267#M166366</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-27T15:01:48Z</dc:date>
    </item>
  </channel>
</rss>

