<?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 Accessing different Variables with varying names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178072#M302534</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a table like this:&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" style="border: 1px solid #000000; width: 100%;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2014&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2015&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2016&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2017&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2018&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2019&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;100&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;200&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;50&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;30&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;25&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;17&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to create a data step and have to access the columns depending on the input date. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data test;&lt;/P&gt;&lt;P&gt;set AK_Table;&lt;/P&gt;&lt;P&gt;y=year(input_date);&lt;/P&gt;&lt;P&gt;If &lt;STRONG&gt;AK_y&lt;/STRONG&gt; &amp;gt;0 then;&lt;/P&gt;&lt;P&gt;Result1=AK_y;&lt;/P&gt;&lt;P&gt;Else if AK_(y-1) &amp;gt;0 then;&lt;/P&gt;&lt;P&gt;Result1=AK_(y-1);&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;But obviously this is not the correct way, since SAS tries to find the variable AK_y, which does not exist.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can I get the value of y and access the correct variable? For example if y=2015, then access AK_2015 and (if necessary) AK_2014, if y=2018, access AK_2018 and (if necessary) AK_2017 and so on...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you in advance for any help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&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;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 12 Jan 2015 09:24:49 GMT</pubDate>
    <dc:creator>andreas_schmitz</dc:creator>
    <dc:date>2015-01-12T09:24:49Z</dc:date>
    <item>
      <title>Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178072#M302534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a table like this:&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" style="border: 1px solid #000000; width: 100%;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2014&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2015&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2016&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2017&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2018&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AK_2019&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;100&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;200&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;50&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;30&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;25&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;17&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to create a data step and have to access the columns depending on the input date. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data test;&lt;/P&gt;&lt;P&gt;set AK_Table;&lt;/P&gt;&lt;P&gt;y=year(input_date);&lt;/P&gt;&lt;P&gt;If &lt;STRONG&gt;AK_y&lt;/STRONG&gt; &amp;gt;0 then;&lt;/P&gt;&lt;P&gt;Result1=AK_y;&lt;/P&gt;&lt;P&gt;Else if AK_(y-1) &amp;gt;0 then;&lt;/P&gt;&lt;P&gt;Result1=AK_(y-1);&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;But obviously this is not the correct way, since SAS tries to find the variable AK_y, which does not exist.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can I get the value of y and access the correct variable? For example if y=2015, then access AK_2015 and (if necessary) AK_2014, if y=2018, access AK_2018 and (if necessary) AK_2017 and so on...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you in advance for any help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&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;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 09:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178072#M302534</guid>
      <dc:creator>andreas_schmitz</dc:creator>
      <dc:date>2015-01-12T09:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178073#M302535</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Arrays are probably the easiest.&amp;nbsp; I would suggest that you remove the 201x from your variable names and just use a sequential number with the year in the label, it will make your life easier and your code look better.&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array ak{6} 8. ak_2014-ak2019;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y=year(input_date);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result=ak{y-2013};&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now I haven't checked the above runs as I don't have test data, but what it does is take 2013 off your year value to leave 1-9 and use that to reference the specific variable in the array.&amp;nbsp; The reason I mention sequential numbers is that if you have a date with 2013 then your going to have problems.&amp;nbsp; If its just 1-9 and 1 references the first date in the row, then the code would work in all cases, and you then just need to check the label for ak_1 to get the year.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 10:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178073#M302535</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-01-12T10:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178074#M302536</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input a_2014 a_2013 ;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1 4&lt;/P&gt;&lt;P&gt;0 7&lt;/P&gt;&lt;P&gt;5 8&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;&lt;/P&gt;&lt;P&gt;%let YEAR = 2014;&lt;/P&gt;&lt;P&gt;%let PYEAR =&amp;nbsp; %sysfunc(putn(&amp;amp;YEAR-1,4.));&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;if a_&amp;amp;YEAR &amp;gt; 0 then result1=a_&amp;amp;YEAR;&lt;/P&gt;&lt;P&gt;Else if a_&amp;amp;PYEAR &amp;gt; 0 then result1=a_&amp;amp;PYEAR;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 10:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178074#M302536</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2015-01-12T10:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178075#M302537</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is &lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;input_date&lt;/SPAN&gt; a simple value outside the dataset&amp;nbsp; OR it is a variable include in the dataset ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 10:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178075#M302537</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-01-12T10:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178076#M302538</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;input_date is a variable the user has to input via prompt in SAS.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 12:03:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178076#M302538</guid>
      <dc:creator>andreas_schmitz</dc:creator>
      <dc:date>2015-01-12T12:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178077#M302539</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So you are using EG . better post is at&amp;nbsp; &lt;A _jive_internal="true" data-containerid="2007" data-containertype="14" data-objectid="10" data-objecttype="14" href="https://communities.sas.com/community/support-communities/sas_enterprise_guide" style="color: #0e66ba; background-color: #f0f8fb;"&gt;SAS Enterprise Guide&lt;/A&gt;. &lt;/P&gt;&lt;P&gt;And mohamoed's code might be what you are looking for .&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 12:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178077#M302539</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-01-12T12:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178078#M302540</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok, I'll do this next time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ok, but I also liked the suggestion of RW9 and used an array since I don't have values before 2014.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you all guys for your help. You are simply awesome.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 12:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178078#M302540</guid>
      <dc:creator>andreas_schmitz</dc:creator>
      <dc:date>2015-01-12T12:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing different Variables with varying names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178079#M302541</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Then, please mark the question as answered.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Jan 2015 13:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accessing-different-Variables-with-varying-names/m-p/178079#M302541</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2015-01-12T13:42:10Z</dc:date>
    </item>
  </channel>
</rss>

