<?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 column format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705621#M216509</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You can't have digit as a first symbol of variable name (unless you have the `validvarname=any` option enabled).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If&amp;nbsp;validvarname is set to `any` you will have to call to your variables names with quotes ended with `n` in your code, e.g. "201201"n.&lt;/P&gt;
&lt;P&gt;Hyphen (`-`) is also not allowed in standard session setting, I would reccomend replace it with underscore(`_`), other wise you will have to use the "..."n approach.&lt;/P&gt;
&lt;P&gt;With that "warning" said, your code could be something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=ANY;
data work.have;
"201201"n = 1;
"201202"n = 1;
"201203"n = 1; 
"201204"n = 1; 
"201205"n = 1; 
"201301"n = 1; 
"201302"n = 1; 
"201303"n = 1; 
"201304"n = 1;
run;

data _null_;
  set work.have;
  array vars _numeric_ ;

  length monyear $ 8 replace $ 32767;
  do over vars;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &amp;amp;replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Mon, 14 Dec 2020 09:40:06 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-12-14T09:40:06Z</dc:date>
    <item>
      <title>Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705614#M216503</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I have a columns name like below:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;Description 201201&amp;nbsp; 201202&amp;nbsp; 201203&amp;nbsp; 201204&amp;nbsp; 201205&amp;nbsp; 201301&amp;nbsp; 201302&amp;nbsp; 201303&amp;nbsp; 201304..........etc.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Now i want to change column name dynamically from 201201 to 201304&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;into following format&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;Jan-2012&amp;nbsp; Feb-2012 Mar-2012 Apr-2012 May-2012 Jan-2013 Feb-2013 Mar-2013 Apr-2013.......etc.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Harsh&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 09:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705614#M216503</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-14T09:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705618#M216506</link>
      <description>&lt;P&gt;Do you want to do this in your actual SAS data or just in Proc Report?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 09:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705618#M216506</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-12-14T09:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705619#M216507</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Actually i want to do in actual data set&lt;/P&gt;
&lt;P&gt;But you can guide me in both the way if possible&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 09:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705619#M216507</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-14T09:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705621#M216509</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You can't have digit as a first symbol of variable name (unless you have the `validvarname=any` option enabled).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If&amp;nbsp;validvarname is set to `any` you will have to call to your variables names with quotes ended with `n` in your code, e.g. "201201"n.&lt;/P&gt;
&lt;P&gt;Hyphen (`-`) is also not allowed in standard session setting, I would reccomend replace it with underscore(`_`), other wise you will have to use the "..."n approach.&lt;/P&gt;
&lt;P&gt;With that "warning" said, your code could be something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=ANY;
data work.have;
"201201"n = 1;
"201202"n = 1;
"201203"n = 1; 
"201204"n = 1; 
"201205"n = 1; 
"201301"n = 1; 
"201302"n = 1; 
"201303"n = 1; 
"201304"n = 1;
run;

data _null_;
  set work.have;
  array vars _numeric_ ;

  length monyear $ 8 replace $ 32767;
  do over vars;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &amp;amp;replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 09:40:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705621#M216509</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-12-14T09:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705630#M216514</link>
      <description>&lt;P&gt;Thanks for your response,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;But when i am submiting below program with your guidance then i am getting below error.&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;data have;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;retain ID Description '201201'n '201202'n '201203'n 0;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;One of my column name id automatically renamed as (- .) , but it should named id only&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 11:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705630#M216514</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-14T11:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705631#M216515</link>
      <description>&lt;P&gt;Replace this part:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;array vars _numeric_ ;&lt;/LI-CODE&gt;
&lt;P&gt;with something like this:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;array vars '201201'n -- '201203'n;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 11:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705631#M216515</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-12-14T11:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705633#M216516</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;But now description column is showing (- .)&lt;/P&gt;
&lt;P&gt;Please find the below screenshot&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="harshpatel_0-1607945446461.png" style="width: 756px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52565iE9662F9CCF61880B/image-dimensions/756x131?v=v2" width="756" height="131" role="button" title="harshpatel_0-1607945446461.png" alt="harshpatel_0-1607945446461.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 11:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705633#M216516</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-14T11:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705641#M216518</link>
      <description>&lt;P&gt;I see correct result. Did you run it like that?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
retain ID Description '201201'n '201202'n '201203'n 0;
run;

data _null_;
  set work.have;
  array vars _numeric_ ;

  length monyear $ 8 replace $ 32767;
  do over array vars '201201'n -- '201203'n;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &amp;amp;replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Are the variables in that exact order in the data set:&amp;nbsp;&lt;CODE class=" language-sas"&gt;ID Description '201201'n '201202'n '201203'n&amp;nbsp;?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;What Log says?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;B&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 11:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705641#M216518</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-12-14T11:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705668#M216526</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;It is showing below error&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="harshpatel_0-1607952161379.png" style="width: 814px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52574i27C8EBFABF9A01E0/image-dimensions/814x182?v=v2" width="814" height="182" role="button" title="harshpatel_0-1607952161379.png" alt="harshpatel_0-1607952161379.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 13:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705668#M216526</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-14T13:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705671#M216527</link>
      <description>&lt;P&gt;Sorry, my mistake. I pasted it wrong. This is the code that should be executed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
retain ID Description '201201'n '201202'n '201203'n 0;
run;

data _null_;
  set work.have;
  array vars '201201'n -- '201203'n;

  length monyear $ 8 replace $ 32767;
  do over vars;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &amp;amp;replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Dec 2020 13:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705671#M216527</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-12-14T13:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705713#M216534</link>
      <description>&lt;P&gt;Why do you want to go from one set of bad names to another?&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway let's setup an example dataset since you didn't provide one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=any;
data have;
 length ID $12  Description $20 '201201'n-'201212'n  8;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So to convert those YYYYMM strings into actual DATE values use the YYMMN informat.&amp;nbsp; Then to generate the MON-YYYY strings you can use the MONNAME and YEAR formats.&amp;nbsp; You can use the DICTIONARY.COLUMNS metadata view to find the names.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('-',put(input(name,yymmn6.),monname3.),put(input(name,yymmn6.),year4.)) as date
     , catx('=',nliteral(name),nliteral(calculated date))
  into :renames,:renames separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE'
    and 0=notdigit(trim(name))
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resutls:&lt;/P&gt;
&lt;PRE&gt; %put &amp;amp;=renames ;
RENAMES="201201"N="Jan-2012"N "201202"N="Feb-2012"N "201203"N="Mar-2012"N "201204"N="Apr-2012"N
"201205"N="May-2012"N "201206"N="Jun-2012"N "201207"N="Jul-2012"N "201208"N="Aug-2012"N
"201209"N="Sep-2012"N "201210"N="Oct-2012"N "201211"N="Nov-2012"N "201212"N="Dec-2012"N
&lt;/PRE&gt;
&lt;P&gt;Now you use that list in a&amp;nbsp;RENAME statement (or the RENAME= dataset option) to rename the variables in the next step you run.&amp;nbsp; Or to rename in place use PROC DATASETS to modify the dataset and change the names without re-writing the actual data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets lib=work nolist;
  modify have;
    rename &amp;amp;renames ;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But even with the new names your dataset is still not very useful.&amp;nbsp; Instead you should transpose the data so that the DATE values are stored as data and not as metadata (variable NAME).&amp;nbsp; For example you could call the new variable MONTH since it contains month values. Then give the COL1 that PROC TRANSPOSE will create to store the values you have the variables with dates as names a name that actually reflects what the values mean.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want;
  by id description;
run;
data want ;
  set want;
  month= input(_name_,yymmn6.);
  format month monyy7. ;
  rename col1 = Real_Name ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Dec 2020 14:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705713#M216534</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-14T14:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705923#M216617</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Great, it works for me&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 04:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705923#M216617</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-15T04:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Change column format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705924#M216618</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Superb, it works for me&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 04:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-format/m-p/705924#M216618</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-15T04:50:17Z</dc:date>
    </item>
  </channel>
</rss>

