<?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 Proc sql: dynamically selecting column based on input data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213709#M52699</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I want to dynamically select the column of an input data set in a PROC SQL procedure. &lt;SPAN style="font-size: 13.3333330154419px;"&gt;I am sure it is an easy thing to do, but &lt;/SPAN&gt;I am not sure how to do it. I have following code: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data index;&lt;/P&gt;&lt;P&gt;input date date9. A B C;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;01JAN2015 1 2 3&lt;/P&gt;&lt;P&gt;02JAN2015 2 3 4&lt;/P&gt;&lt;P&gt;03JAN2015 3 4 5&lt;/P&gt;&lt;P&gt;;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data choice;&lt;/P&gt;&lt;P&gt;input id date date9. choice $;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 02JAN2015 A&lt;/P&gt;&lt;P&gt;2 02JAN2015 B&lt;/P&gt;&lt;P&gt;3 03JAN2015 A&lt;/P&gt;&lt;P&gt;4 03JAN2015 C&lt;/P&gt;&lt;P&gt;; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql; &lt;/P&gt;&lt;P&gt;&amp;nbsp; create table choice_with_index as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select choice.*, index.*&lt;/P&gt;&lt;P&gt;&amp;nbsp; from choice as a, index as b&lt;/P&gt;&lt;P&gt;&amp;nbsp; where a.date = b.date; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Instead of selecting all the columns of the index data set (which I do with select index.*), I only want to select the column that corresponds to the value of the variable choice in the choice data set. In other words, I only want the following data set:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want; &lt;/P&gt;&lt;P&gt;input id date date9. choice $ chosen_index;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 02JAN2015 A 2&lt;/P&gt;&lt;P&gt;2 02JAN2015 B 3&lt;/P&gt;&lt;P&gt;3 03JAN2015 A 3&lt;/P&gt;&lt;P&gt;4 03JAN2015 C 5&lt;/P&gt;&lt;P&gt;; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I said, I suppose this is really easy to do. Could you also tell me what this technique is called? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 01 Apr 2015 00:36:49 GMT</pubDate>
    <dc:creator>iank131</dc:creator>
    <dc:date>2015-04-01T00:36:49Z</dc:date>
    <item>
      <title>Proc sql: dynamically selecting column based on input data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213709#M52699</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I want to dynamically select the column of an input data set in a PROC SQL procedure. &lt;SPAN style="font-size: 13.3333330154419px;"&gt;I am sure it is an easy thing to do, but &lt;/SPAN&gt;I am not sure how to do it. I have following code: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data index;&lt;/P&gt;&lt;P&gt;input date date9. A B C;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;01JAN2015 1 2 3&lt;/P&gt;&lt;P&gt;02JAN2015 2 3 4&lt;/P&gt;&lt;P&gt;03JAN2015 3 4 5&lt;/P&gt;&lt;P&gt;;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data choice;&lt;/P&gt;&lt;P&gt;input id date date9. choice $;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 02JAN2015 A&lt;/P&gt;&lt;P&gt;2 02JAN2015 B&lt;/P&gt;&lt;P&gt;3 03JAN2015 A&lt;/P&gt;&lt;P&gt;4 03JAN2015 C&lt;/P&gt;&lt;P&gt;; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql; &lt;/P&gt;&lt;P&gt;&amp;nbsp; create table choice_with_index as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select choice.*, index.*&lt;/P&gt;&lt;P&gt;&amp;nbsp; from choice as a, index as b&lt;/P&gt;&lt;P&gt;&amp;nbsp; where a.date = b.date; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Instead of selecting all the columns of the index data set (which I do with select index.*), I only want to select the column that corresponds to the value of the variable choice in the choice data set. In other words, I only want the following data set:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want; &lt;/P&gt;&lt;P&gt;input id date date9. choice $ chosen_index;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 02JAN2015 A 2&lt;/P&gt;&lt;P&gt;2 02JAN2015 B 3&lt;/P&gt;&lt;P&gt;3 03JAN2015 A 3&lt;/P&gt;&lt;P&gt;4 03JAN2015 C 5&lt;/P&gt;&lt;P&gt;; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I said, I suppose this is really easy to do. Could you also tell me what this technique is called? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Apr 2015 00:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213709#M52699</guid>
      <dc:creator>iank131</dc:creator>
      <dc:date>2015-04-01T00:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: dynamically selecting column based on input data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213710#M52700</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Please try&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data index;&lt;/P&gt;&lt;P&gt;input date date9. A B C;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;01JAN2015 1 2 3&lt;/P&gt;&lt;P&gt;02JAN2015 2 3 4&lt;/P&gt;&lt;P&gt;03JAN2015 3 4 5&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc transpose data=index out=index(rename=_name_=choice);&lt;/P&gt;&lt;P&gt;by date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data choice;&lt;/P&gt;&lt;P&gt;input id date date9. choice $;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 02JAN2015 A&lt;/P&gt;&lt;P&gt;2 02JAN2015 B&lt;/P&gt;&lt;P&gt;3 03JAN2015 A&lt;/P&gt;&lt;P&gt;4 03JAN2015 C&lt;/P&gt;&lt;P&gt;; &lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table choice_with_index as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select choice.*,index.col1 as chosen_index&lt;/P&gt;&lt;P&gt;&amp;nbsp; from choice as a left join index as b&lt;/P&gt;&lt;P&gt;&amp;nbsp; on a.date = b.date and a.choice=b.choice; &lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Apr 2015 01:17:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213710#M52700</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2015-04-01T01:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: dynamically selecting column based on input data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213711#M52701</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;VVALUEX function is also an option, but the transpose is more succinct if you have a longer variable list to check:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #011993;"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; want;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;merge&lt;/SPAN&gt; choice (&lt;SPAN style="color: #0433ff;"&gt;in&lt;/SPAN&gt;=in_c) index;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;by&lt;/SPAN&gt; date;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;if&lt;/SPAN&gt; in_C;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;choice_index=input(compress(vvaluex(choice)), &lt;SPAN style="color: #009193;"&gt;&lt;STRONG&gt;2.&lt;/STRONG&gt;&lt;/SPAN&gt;);&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #0433ff;"&gt;drop&lt;/SPAN&gt; A B C;&lt;/P&gt;&lt;P style="font-size: 10px; font-family: 'Courier New'; color: #011993;"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN style="color: #000000;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Apr 2015 01:50:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213711#M52701</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-04-01T01:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: dynamically selecting column based on input data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213712#M52702</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for Reeza's helpful answer. &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;All I need to do is add to my original code following the PROC SQL is the following code that uses the VVALUEX function and I get the data set I want:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want (drop= A B C);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set choice_with_index;&lt;/P&gt;&lt;P&gt;&amp;nbsp; chosen_index = input( compress(vvaluex(choice)), 2.); run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BTW, I tried putting it inside the PROC SQL, but I get an error since the VVALUEX function only works in data steps. &lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://communities.sas.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I didn't know about the VVALUEX function, I found the following post most brief and informative in explaining it:&lt;/P&gt;&lt;P&gt;&lt;A href="https://growthtrajectory.wordpress.com/2013/03/07/sas-tip-dynamic-variable-name-vvaluex/" title="https://growthtrajectory.wordpress.com/2013/03/07/sas-tip-dynamic-variable-name-vvaluex/"&gt;SAS TIP: Dynamic Variable Name &amp;amp;#8212; VValuex | TRAJECTORY&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Apr 2015 02:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-dynamically-selecting-column-based-on-input-data/m-p/213712#M52702</guid>
      <dc:creator>iank131</dc:creator>
      <dc:date>2015-04-01T02:46:32Z</dc:date>
    </item>
  </channel>
</rss>

