<?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: Create variable names in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114846#M292875</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Chris,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your reply was very helpful. Thanks. To avoid writing several array statements (in my case up to 13) I used a multidimensional array. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let min = 11;&lt;/P&gt;&lt;P&gt; %let max = 13;&lt;/P&gt;&lt;P&gt; array arr{2,20&amp;amp;min:20&amp;amp;max} net_qu&amp;amp;min-net_qu&amp;amp;max sale_nb&amp;amp;min-sale;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;TD&gt;arr{1,uwyear} = net_qu;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt; arr{2,uwyear} = sale_nb;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So next year I just have to change my min and max values. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But there are still some variables that seems not that easy to handle. Here the year is in the middle of the variable. e.g. sale_11_rn .&amp;nbsp; So I can't use a numbered range list. The people worked on this report before loved it to hard code everything. Maybe there is also a nice trick to avoid hard coding all the variables without change the names?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Christoph&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 04 Jan 2013 15:22:36 GMT</pubDate>
    <dc:creator>chrwag</dc:creator>
    <dc:date>2013-01-04T15:22:36Z</dc:date>
    <item>
      <title>Create variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114844#M292873</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hallo,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a data set with observations for several years (2010,2011,2012). So there is a variable year and some variables with premiums and so on. Now I try to process the data set and create particular variables for each year. e.g.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;if uwyear = 2012 then do;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; premium12 = premium;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ......&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;ELSE DO;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; premium12 = 0;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;END;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This leads to a lot of coding which I try to avoid. My idea was to process the assignment statements in a loop. But I am stuck on how to create the variable names depending on the index variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;DO i = 2010 to 2012;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; premium&amp;amp;i = premium;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;END;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried a lot of combinations with rename and call symput. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to this? Hope someone can help me. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Christoph&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Jan 2013 10:13:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114844#M292873</guid>
      <dc:creator>chrwag</dc:creator>
      <dc:date>2013-01-04T10:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114845#M292874</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;One solution is to use an array...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Simplest form, hard-coded years */&lt;BR /&gt;data new ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set mydata ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array py{2010:2012} premium2010-premium2012 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; yr = year(uwyear) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; py{yr} = premium ;&lt;/P&gt;&lt;P&gt;run ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Dynamic, based on years within data, and initialize to zero */&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;&amp;nbsp; select min(year(uwyear)) into :Y1 from mydata ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; selex max(year(uwyear)) into :Y2 from mydata ;&lt;/P&gt;&lt;P&gt;quit ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data new ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set mydata ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array py{&amp;amp;Y1:&amp;amp;Y2} premium&amp;amp;Y1-premium&amp;amp;Y2 ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* Set to zero */&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i = lbound(py) to hbound(py) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; py{i} = 0 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; py{year(uwyear)} = premium ;&lt;/P&gt;&lt;P&gt;run ;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Jan 2013 11:37:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114845#M292874</guid>
      <dc:creator>chrisj75</dc:creator>
      <dc:date>2013-01-04T11:37:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114846#M292875</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Chris,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your reply was very helpful. Thanks. To avoid writing several array statements (in my case up to 13) I used a multidimensional array. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let min = 11;&lt;/P&gt;&lt;P&gt; %let max = 13;&lt;/P&gt;&lt;P&gt; array arr{2,20&amp;amp;min:20&amp;amp;max} net_qu&amp;amp;min-net_qu&amp;amp;max sale_nb&amp;amp;min-sale;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;TD&gt;arr{1,uwyear} = net_qu;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt; arr{2,uwyear} = sale_nb;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So next year I just have to change my min and max values. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But there are still some variables that seems not that easy to handle. Here the year is in the middle of the variable. e.g. sale_11_rn .&amp;nbsp; So I can't use a numbered range list. The people worked on this report before loved it to hard code everything. Maybe there is also a nice trick to avoid hard coding all the variables without change the names?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Christoph&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Jan 2013 15:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114846#M292875</guid>
      <dc:creator>chrwag</dc:creator>
      <dc:date>2013-01-04T15:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114847#M292876</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok... similar to before, use a PROC SQL to get the distinct years in the data, but instead create a list of variable names...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select distinct(cats('sale_',year(uwyear),'_rn')) into :VAR1 separated by ' ' ;&lt;/P&gt;&lt;P&gt;quit ;&lt;/P&gt;&lt;P&gt;%PUT &amp;amp;VAR1 ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;VAR1 resolves to&lt;/P&gt;&lt;P&gt;&amp;nbsp; sale_10_rn sale_11_rn sale_12_rn&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then use that to define the variables for an array, i.e. (assuming &amp;amp;MIN = 10, &amp;amp;MAX = 12)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array sale{&amp;amp;MIN,&amp;amp;MAX} &amp;amp;VAR1 ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope that helps.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Jan 2013 15:38:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114847#M292876</guid>
      <dc:creator>chrisj75</dc:creator>
      <dc:date>2013-01-04T15:38:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114848#M292877</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It may be a pain but it could pay lots of dividends to change naming systems like sale_11_rn sale_12_rn to sale_rn_11 sale_rn_12. Every time I've gotten involved with a middle of variable scheme with chaning numbers it has lead to lots of headaches.&lt;/P&gt;&lt;P&gt;I'm also paranoid having had to deal with Y2K coding and would recommend using 4 digit year naming so there is no confusion in the inevitable stem_11_12 type named variable.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Jan 2013 15:46:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114848#M292877</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2013-01-04T15:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114849#M292878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry for my late response. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;@chris:&lt;/P&gt;&lt;P&gt;Thanks. The Proc sql solution helped a lot.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;@ballardw:&lt;/P&gt;&lt;P&gt;Of Course to change the variable names would be the best solution. But due to a lot of dependencies changing names is not that easy in this case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have a good weekend. &lt;/P&gt;&lt;P&gt;Christoph&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Jan 2013 08:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-names-in-a-loop/m-p/114849#M292878</guid>
      <dc:creator>chrwag</dc:creator>
      <dc:date>2013-01-18T08:15:59Z</dc:date>
    </item>
  </channel>
</rss>

