<?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: How can I use a variable name as column name? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567315#M11477</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278427"&gt;@erickdt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I need, put the rating in my database PARCELADOS_V1 based on my working day and i think that i cannot use transpose or array &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can do it easily in a data step, no transpose needed actually and it's seems pretty straight forward and dynamic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll want to modify the KEEP statement int he second data set to keep only the variables you need.&lt;/P&gt;
&lt;P&gt;You may need to modify the array index for the issues I identified in my last post or you may need to use an IF condition. I didn't have any data to test on so there are likely small bugs in places but it gives you an idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=pda.rating_&amp;amp;mesref;
by cpfcnpj;
run;

proc sort data=parcedalos_v1;
by cpf_cnpj;
run;

data want;
merge parcelados_v1 (in=a) pda.rating_&amp;amp;mesref (rename=cpf_cnpj=cpfcnpj /*keep = (du_aux )*/ );
by cpfcnpj;
if a; *left join;
array _d(-2:19) du_003 du_002 du_001 du_01-du_19;

rating = _d(du_aux);

run;



&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278427"&gt;@erickdt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;thanks tom!&lt;/P&gt;
&lt;P&gt;I'll try explain better now, yesterday i was running out of my head.....&lt;BR /&gt;lol&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;In my database:&amp;nbsp;&amp;nbsp;PARCELADOS_V1 I've all my clientes that made some purchase and the working day, aproximattly 3000&lt;BR /&gt;In my database:&amp;nbsp;PDA.RATING_&amp;amp;MESREF. I've all clients from my database (+60MM) and the rating for each working day, beacause they can change rating between days....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I need, put the rating in my database PARCELADOS_V1 based on my working day and i think that i cannot use transpose or array &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks again!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jun 2019 15:15:13 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-19T15:15:13Z</dc:date>
    <item>
      <title>How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567082#M11439</link>
      <description>&lt;P&gt;I've a problem here, I need to use the name of the columns depends on my working day, as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CASE WHEN DU_AUX = 1 THEN B.DU_01&lt;BR /&gt;WHEN DU_AUX = 2 THEN B.DU_02&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;however i want to optimize my code and not depends on my hands to do it, can anyone help me with this problem?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should be like this:&lt;/P&gt;&lt;P&gt;CASE&amp;nbsp; WHEN DU_AUX = 1 THEN B.DU_"&amp;amp;01."&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHEN DU_AUX = 2 THEN B.DU_"&amp;amp;02."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where "&amp;amp;01" should be the value in DU_AUX.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;MY COMPLETE SELECT NOWADAYS:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE PARCELADOS_&amp;amp;MESREF._1 (drop = DU_AUX) AS SELECT&lt;BR /&gt;A.*&lt;BR /&gt;,CASE WHEN DU_AUX = -2 THEN B.DU_003&lt;BR /&gt;WHEN DU_AUX = -1 THEN B.DU_002&lt;BR /&gt;WHEN DU_AUX = 0 THEN B.DU_001&lt;BR /&gt;WHEN DU_AUX = 1 THEN B.DU_01&lt;BR /&gt;WHEN DU_AUX = 2 THEN B.DU_02&lt;BR /&gt;WHEN DU_AUX = 3 THEN B.DU_03&lt;BR /&gt;WHEN DU_AUX = 4 THEN B.DU_04&lt;BR /&gt;WHEN DU_AUX = 5 THEN B.DU_05&lt;BR /&gt;WHEN DU_AUX = 6 THEN B.DU_06&lt;BR /&gt;WHEN DU_AUX = 7 THEN B.DU_07&lt;BR /&gt;WHEN DU_AUX = 8 THEN B.DU_08&lt;BR /&gt;WHEN DU_AUX = 9 THEN B.DU_09&lt;BR /&gt;WHEN DU_AUX = 10 THEN B.DU_10&lt;BR /&gt;WHEN DU_AUX = 11 THEN B.DU_11&lt;BR /&gt;WHEN DU_AUX = 12 THEN B.DU_12&lt;BR /&gt;WHEN DU_AUX = 13 THEN B.DU_13&lt;BR /&gt;WHEN DU_AUX = 14 THEN B.DU_14&lt;BR /&gt;WHEN DU_AUX = 15 THEN B.DU_15&lt;BR /&gt;WHEN DU_AUX = 16 THEN B.DU_16&lt;BR /&gt;WHEN DU_AUX = 17 THEN B.DU_17&lt;BR /&gt;WHEN DU_AUX = 18 THEN B.DU_18&lt;BR /&gt;WHEN DU_AUX = 19 THEN B.DU_19&lt;BR /&gt;/* WHEN DU_AUX = 20 THEN B.DU_20*/&lt;BR /&gt;END AS RATING&lt;BR /&gt;FROM PARCELADOS_V1 AS A&lt;BR /&gt;LEFT JOIN PDA.RATING_&amp;amp;MESREF. AS B&lt;BR /&gt;ON A.CPFCNPJ = B.CPF_CNPJ;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 21:58:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567082#M11439</guid>
      <dc:creator>erickdt</dc:creator>
      <dc:date>2019-06-18T21:58:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567084#M11441</link>
      <description>&lt;P&gt;Why are you using PROC SQL to transpose your data?&lt;/P&gt;
&lt;P&gt;Why not just pull out the data and then use PROC TRANSPOSE?&lt;/P&gt;
&lt;P&gt;Or use a data step?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array DU [-2:20] du_003-du_001 du_01-du_20 ;
  if -2 &amp;lt;= du_aux &amp;lt;= 20 then rating=du[du_aux];
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 22:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567084#M11441</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-18T22:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567107#M11443</link>
      <description>Agreeing with Tom, you're using the wrong tool here and making your life harder. Use PROC TRANSPOSE within SAS. &lt;BR /&gt;&lt;BR /&gt;In a different SQL environment you would use PIVOT (or another similar option) so this isn't a really good solution in any language.</description>
      <pubDate>Wed, 19 Jun 2019 01:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567107#M11443</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-19T01:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567232#M11464</link>
      <description>&lt;P&gt;thanks tom!&lt;/P&gt;&lt;P&gt;I'll try explain better now, yesterday i was running out of my head.....&lt;BR /&gt;lol&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In my database:&amp;nbsp;&amp;nbsp;PARCELADOS_V1 I've all my clientes that made some purchase and the working day, aproximattly 3000&lt;BR /&gt;In my database:&amp;nbsp;PDA.RATING_&amp;amp;MESREF. I've all clients from my database (+60MM) and the rating for each working day, beacause they can change rating between days....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I need, put the rating in my database PARCELADOS_V1 based on my working day and i think that i cannot use transpose or array &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks again!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 12:45:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567232#M11464</guid>
      <dc:creator>erickdt</dc:creator>
      <dc:date>2019-06-19T12:45:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567312#M11476</link>
      <description>&lt;P&gt;Your logic doesn't appear to hold:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CASE&amp;nbsp; WHEN DU_AUX = 1 THEN B.DU_"&amp;amp;01."&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHEN DU_AUX = 2 THEN B.DU_"&amp;amp;02."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4" color="#800080"&gt;&lt;STRONG&gt;where "&amp;amp;01" should be the value in DU_AUX.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But your code has the following which doesn't match the logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;CASE WHEN DU_AUX = -2 THEN B.DU_003&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WHEN DU_AUX = -1 THEN B.DU_002&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WHEN DU_AUX = 0 THEN B.DU_001&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;SPAN&gt;WHEN DU_AUX = 1 THEN B.DU_01&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;WHEN DU_AUX = 2 THEN B.DU_02&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 15:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567312#M11476</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-19T15:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567315#M11477</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278427"&gt;@erickdt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I need, put the rating in my database PARCELADOS_V1 based on my working day and i think that i cannot use transpose or array &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can do it easily in a data step, no transpose needed actually and it's seems pretty straight forward and dynamic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll want to modify the KEEP statement int he second data set to keep only the variables you need.&lt;/P&gt;
&lt;P&gt;You may need to modify the array index for the issues I identified in my last post or you may need to use an IF condition. I didn't have any data to test on so there are likely small bugs in places but it gives you an idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=pda.rating_&amp;amp;mesref;
by cpfcnpj;
run;

proc sort data=parcedalos_v1;
by cpf_cnpj;
run;

data want;
merge parcelados_v1 (in=a) pda.rating_&amp;amp;mesref (rename=cpf_cnpj=cpfcnpj /*keep = (du_aux )*/ );
by cpfcnpj;
if a; *left join;
array _d(-2:19) du_003 du_002 du_001 du_01-du_19;

rating = _d(du_aux);

run;



&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278427"&gt;@erickdt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;thanks tom!&lt;/P&gt;
&lt;P&gt;I'll try explain better now, yesterday i was running out of my head.....&lt;BR /&gt;lol&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;In my database:&amp;nbsp;&amp;nbsp;PARCELADOS_V1 I've all my clientes that made some purchase and the working day, aproximattly 3000&lt;BR /&gt;In my database:&amp;nbsp;PDA.RATING_&amp;amp;MESREF. I've all clients from my database (+60MM) and the rating for each working day, beacause they can change rating between days....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I need, put the rating in my database PARCELADOS_V1 based on my working day and i think that i cannot use transpose or array &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks again!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 15:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567315#M11477</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-19T15:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable name as column name?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567371#M11494</link>
      <description>&lt;P&gt;Tom, thanks SO MUCH!!!!!!! I've made some changes! but it works &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;&lt;P&gt;thanks&amp;nbsp;thanks&amp;nbsp;thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA WANT (KEEP= CPFCNPJ &amp;amp;VARS. RATING);&lt;BR /&gt;MERGE PARCELADOS_V1 (IN=A) PDA.RATING_&amp;amp;MESREF (RENAME=CPF_CNPJ=CPFCNPJ);&lt;BR /&gt;BY CPFCNPJ;&lt;BR /&gt;IF A;&lt;BR /&gt;ARRAY _D(-2:&amp;amp;DU.) DU_003 DU_002 DU_001 DU_01-&amp;amp;MAXDU.;&lt;BR /&gt;RATING = _D(DU_AUX);&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 17:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-use-a-variable-name-as-column-name/m-p/567371#M11494</guid>
      <dc:creator>erickdt</dc:creator>
      <dc:date>2019-06-19T17:27:31Z</dc:date>
    </item>
  </channel>
</rss>

