<?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 Resolving a macro variable to a column name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170912#M32813</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm have a dataset with sequentially numbered variable (column) names that contain data for different years. I want to compile all of these data into two different variables -- call them PreviousYear and CurrentYear -- based on which year it currently is in the dataset. In the data snippet below, for example, the column ENR_F03 would be the current year's data for the year 2003, but the previous year's data for the year 2004. So for the first observation, I would like PreviousYear to take the value from column ENR_F02, and CurrentYear to take the value from column ENR_F03. But in the second observation, I would like PreviousYear to take a value from column ENR_F03, and CurrentYear to take the value from ENR_F04, etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD colspan="1"&gt;ENR_F02&lt;/TD&gt;&lt;TD colspan="1"&gt;ENR_F03&lt;/TD&gt;&lt;TD&gt;ENR_F04&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;2003&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;2004&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have started with the following code, which may not be the best approach here, but it raises a question for me. Is it possible to resolve a macro variable as a column name? So far, I have been able to put the column names I want into the variables, but when I try to get the values of those columns back out, all I get is the value of the macro variable itself. So PreviousYear and CurrentYear end up being ENR_F02 and ENR_F03, etc. I could probably code this as a series of if statements that pull data from different columns, but I thought I would see if there's a better way. Thanks for your help!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;set data;&lt;/P&gt;&lt;P&gt;CY=PUT(wage_yr,4.);&lt;/P&gt;&lt;P&gt;PY=PUT(wage_yr-1,4.);&lt;/P&gt;&lt;P&gt;call symput('ENPY','ENR_F'||substr(PY,3,2));&lt;/P&gt;&lt;P&gt;call symput('ENCY','ENR_F'||substr(CY,3,2));&lt;/P&gt;&lt;P&gt;PreviousYear = symget('ENPY');&lt;/P&gt;&lt;P&gt;CurrentYear= symget('ENCY');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 11 Feb 2014 21:17:23 GMT</pubDate>
    <dc:creator>ebowen</dc:creator>
    <dc:date>2014-02-11T21:17:23Z</dc:date>
    <item>
      <title>Resolving a macro variable to a column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170912#M32813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm have a dataset with sequentially numbered variable (column) names that contain data for different years. I want to compile all of these data into two different variables -- call them PreviousYear and CurrentYear -- based on which year it currently is in the dataset. In the data snippet below, for example, the column ENR_F03 would be the current year's data for the year 2003, but the previous year's data for the year 2004. So for the first observation, I would like PreviousYear to take the value from column ENR_F02, and CurrentYear to take the value from column ENR_F03. But in the second observation, I would like PreviousYear to take a value from column ENR_F03, and CurrentYear to take the value from ENR_F04, etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD colspan="1"&gt;ENR_F02&lt;/TD&gt;&lt;TD colspan="1"&gt;ENR_F03&lt;/TD&gt;&lt;TD&gt;ENR_F04&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;2003&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;2004&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have started with the following code, which may not be the best approach here, but it raises a question for me. Is it possible to resolve a macro variable as a column name? So far, I have been able to put the column names I want into the variables, but when I try to get the values of those columns back out, all I get is the value of the macro variable itself. So PreviousYear and CurrentYear end up being ENR_F02 and ENR_F03, etc. I could probably code this as a series of if statements that pull data from different columns, but I thought I would see if there's a better way. Thanks for your help!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;set data;&lt;/P&gt;&lt;P&gt;CY=PUT(wage_yr,4.);&lt;/P&gt;&lt;P&gt;PY=PUT(wage_yr-1,4.);&lt;/P&gt;&lt;P&gt;call symput('ENPY','ENR_F'||substr(PY,3,2));&lt;/P&gt;&lt;P&gt;call symput('ENCY','ENR_F'||substr(CY,3,2));&lt;/P&gt;&lt;P&gt;PreviousYear = symget('ENPY');&lt;/P&gt;&lt;P&gt;CurrentYear= symget('ENCY');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 21:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170912#M32813</guid>
      <dc:creator>ebowen</dc:creator>
      <dc:date>2014-02-11T21:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: Resolving a macro variable to a column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170913#M32814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Perhaps post what your data looks like and what you want. Your method seems roundabout and I'm wondering if there isn't a way to avoid the macros, by using arrays for instance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT: Also look into the vvaluex function for retrieving the value from a variable in a datastep.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002233818.htm" title="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002233818.htm"&gt;SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 21:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170913#M32814</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2014-02-11T21:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: Resolving a macro variable to a column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170914#M32815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unfortunately, I can't post the data, because it's confidential. But it looks pretty much like what I posted -- sequentially numbered columns referring to different years, only the real data has multiple years instead of just the three I put in the sample data. It does seem like there ought to be a way to deal with it in terms of arrays, but I'm not enough of an array aficionado to figure it out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;On your other point, the vvaluex function works well enough for my purposes. I have been able to get the data out of the columns by modifying the data step with that function as follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;data test;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;set data;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;CY=PUT(wage_yr,4.);&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;PY=PUT(wage_yr-1,4.);&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;call symput('ENPY','ENR_F'||substr(PY,3,2));&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;call symput('ENCY','ENR_F'||substr(CY,3,2));&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;PreviousYear = vvaluex(symget('ENPY'));&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;CurrentYear= vvaluex(symget('ENCY'));&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;I would welcome any further comments on this thread to see if there's a better way to handle this.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 22:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170914#M32815</guid>
      <dc:creator>ebowen</dc:creator>
      <dc:date>2014-02-11T22:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: Resolving a macro variable to a column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170915#M32816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Make fake data that mimics your data structure with the output matching the desired output &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;Arrays are still probably a better solution to this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT: You're not using vvaluex properly. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;data test;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;set data;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;CY=PUT(wage_yr,4.);&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;PY=PUT(wage_yr-1,4.);&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;PreviousYear = vvaluex('ENR_F'||substr(PY,3,2));&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;CurrentYear= vvaluex('ENR_F'||substr(CY,3,2));&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 22:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170915#M32816</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2014-02-11T22:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: Resolving a macro variable to a column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170916#M32817</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This looks more like an application for arrays, such as:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array fnr {2002:2004} ENR_F02 ENR_F03 ENR_F04;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if (2002 &amp;lt;= year &amp;lt;= 2004) then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_year = fnr{year};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if year &amp;gt; 2002 then previous_year = fnr{year-1};&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is this in the ballpark for what you are looking to do?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 22:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170916#M32817</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-02-11T22:37:36Z</dc:date>
    </item>
    <item>
      <title>Re: Resolving a macro variable to a column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170917#M32818</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;With some modifications to fit my data, this works great. I figured there had to be a way to do this with arrays. Thanks for the help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Feb 2014 16:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolving-a-macro-variable-to-a-column-name/m-p/170917#M32818</guid>
      <dc:creator>ebowen</dc:creator>
      <dc:date>2014-02-12T16:03:49Z</dc:date>
    </item>
  </channel>
</rss>

