<?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 variables from values in a narrow table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/400271#M278538</link>
    <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/160362"&gt;@ShiroAmada&lt;/a&gt;, your code was very similar to ChrisNZ, so I am sure it would have yielded a successful result as well!</description>
    <pubDate>Mon, 02 Oct 2017 14:22:49 GMT</pubDate>
    <dc:creator>Epigurl</dc:creator>
    <dc:date>2017-10-02T14:22:49Z</dc:date>
    <item>
      <title>Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399630#M278532</link>
      <description>&lt;P&gt;Hi SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 2 datasets. The wide data set has customer sales data by week with column names i.e. Cust_ID, wk_001, wk_002, etc. and the narrow dataset holds the actual week date with column name Col_label. See below for examples. I tried to transpose the wide dataset so the WK_001, WK_002, etc columns are now values and the variable name is Date. Additionally, I want to rename the values in the new "Date" column with the values in the narrow data set column "Col_label".&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc transpose data=wide out=long;
	by cust_id;
	var wk_001-wk_xx;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Narrow Data&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Typ&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Column&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Col_label&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Weekly&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WK_001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Sep_17_08&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Weekly&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WK_002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Sep_17_01&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Wide Data&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Cust_ID&amp;nbsp; &amp;nbsp; &amp;nbsp; WK_001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WK_002&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1001&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;$$&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, the new data set will look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;New data set&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Cust_ID&amp;nbsp; &amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Sales&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;Sep_17_08&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $$&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sep_17_01&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $$&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 20:47:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399630#M278532</guid>
      <dc:creator>Epigurl</dc:creator>
      <dc:date>2017-09-28T20:47:03Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399648#M278533</link>
      <description>&lt;P&gt;I would lean toward a single DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;if _n_=1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; array labels {52} $ 9;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (done);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set narrow (rename=(column=week_char)) end=done;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; week_no = input(substr(week_char, 4), 3.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; labels{week_no} = Col_Label;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For illustration, I assumed 52 weeks, but that is easily changeable.&amp;nbsp; At this point, you have the originally narrow data set in wide form.&amp;nbsp; The same DATA step continues:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set wide;&lt;/P&gt;
&lt;P&gt;array amounts {*} WK_:;&lt;/P&gt;
&lt;P&gt;do i=1 to dim(amounts);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; sales = amounts{i};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; week_no = input(substr(vname(amounts{i}),4), 3.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Date = labels{week_no};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep Cust_ID Date Sales;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, so might need some tweaking.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 21:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399648#M278533</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-28T21:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399662#M278534</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data N;
input TYP $ COLUMN $ COL_LABEL : $9. ;
cards;
Weekly WK_001 Sep_17_08
Weekly WK_002 Sep_17_01
run;
data W;
input CUST_ID  $  WK_001 WK_002 ;
cards;
1001 23 24
run;  
proc transpose data=W out=L;
	by CUST_ID;
	var WK_001-WK_002;
run;
proc sql;
  select CUST_ID, COL_LABEL 'Date', COL1 as SALES 'Sales'  from L,N where L._NAME_=N.COLUMN; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l b header" scope="col"&gt;CUST_ID&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;Date&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;Sales&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;1001&lt;/TD&gt;
&lt;TD class="l data"&gt;Sep_17_08&lt;/TD&gt;
&lt;TD class="r data"&gt;23&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;1001&lt;/TD&gt;
&lt;TD class="l data"&gt;Sep_17_01&lt;/TD&gt;
&lt;TD class="r data"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 23:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399662#M278534</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-09-28T23:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399666#M278535</link>
      <description>&lt;P&gt;Do you just want to use the "narrow" table to assign labels to the columns in the "wide" table?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data narrow ;
 length typ column col_label $32 ;
 input typ column col_label;
cards;
Weekly WK_001 Sep_17_08
Weekly WK_002 Sep_17_01
;
 
data wide ;
  input Cust_id $ WK_001 WK_002 ;
cards;
1001 1 2
1002 2 3
;

proc sql noprint ;
  select catx('=',column,quote(trim(col_label)))
    into :labels separated by ' '
    from narrow
  ;
quit;

proc transpose data=wide out=want (rename=(col1=Sales));
  by cust_id;
  var wk_001 wk_002;
  label &amp;amp;labels ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 291px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15490i8612DD7B440811C2/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 00:26:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399666#M278535</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-29T00:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399677#M278536</link>
      <description>&lt;P&gt;Try this.....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=WIDE;
  by CUSTID;
run;


proc transpose data=WIDE out=W_WIDE(rename= (_NAME_=COLUMN COL1=SALES) );
  var WK_:;
  by CUSTID;
run;

proc sql;
  create table WANT as
  select 
    A.CUSTID,
    B.LABEL as DATE,
    A.SALES 
  from W_WIDE a left join NARROW b
  on upcase(A.COLUMN)=upcase(B.COLUMN);

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note that the values for your variable COLUMN in your source tables may have different lengths.&amp;nbsp; I suggest you standardize using character functions such as upcase, strip...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 01:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/399677#M278536</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-09-29T01:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/400270#M278537</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt; for your help with this SAS code!</description>
      <pubDate>Mon, 02 Oct 2017 14:21:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/400270#M278537</guid>
      <dc:creator>Epigurl</dc:creator>
      <dc:date>2017-10-02T14:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables from values in a narrow table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/400271#M278538</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/160362"&gt;@ShiroAmada&lt;/a&gt;, your code was very similar to ChrisNZ, so I am sure it would have yielded a successful result as well!</description>
      <pubDate>Mon, 02 Oct 2017 14:22:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variables-from-values-in-a-narrow-table/m-p/400271#M278538</guid>
      <dc:creator>Epigurl</dc:creator>
      <dc:date>2017-10-02T14:22:49Z</dc:date>
    </item>
  </channel>
</rss>

