<?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: Creating an infinite number of variables in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62028#M13496</link>
    <description>Perfect Linus.  Thank you so much.  If you were here, I'd give you a hug.</description>
    <pubDate>Thu, 20 Nov 2008 18:58:40 GMT</pubDate>
    <dc:creator>Chiefsfan4ever</dc:creator>
    <dc:date>2008-11-20T18:58:40Z</dc:date>
    <item>
      <title>Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62020#M13488</link>
      <description>Here is my problem.  I have a dataset and it has more than one bucket.  Each row can have 5 buckets and it can essentially have infinite amount of rows.&lt;BR /&gt;
Here is an example of the code I'm trying to write:&lt;BR /&gt;
&lt;BR /&gt;
data work2;&lt;BR /&gt;
set work1;&lt;BR /&gt;
Extra=0;&lt;BR /&gt;
retain extra;&lt;BR /&gt;
NetCount = Maxcount-extra;&lt;BR /&gt;
if ( NetCount &amp;gt;0 ) and (NetCount = count) then do;&lt;BR /&gt;
Bucket||(1+5*(Netcount-1))=Bucket1;&lt;BR /&gt;
Bucket||(2+5*(Netcount-1))=Bucket2;&lt;BR /&gt;
Bucket||(3+5*(Netcount-1))=Bucket3;&lt;BR /&gt;
Bucket||(4+5*(Netcount-1))=bucket4;&lt;BR /&gt;
Bucket||(5+5*(Netcount-1))=bucket5;&lt;BR /&gt;
end;&lt;BR /&gt;
Extra = Extra+1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
As you can see I'm trying to loop through each row and create buckets 1-5, buckets 6-10.  I also have written code to find the Maximum amount of rows needed which is represented by Maxcount (Maxium of count). SAS won't let me create these new variables this way.  &lt;BR /&gt;
&lt;BR /&gt;
ANY IDEAS? &lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance.</description>
      <pubDate>Tue, 18 Nov 2008 15:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62020#M13488</guid>
      <dc:creator>Chiefsfan4ever</dc:creator>
      <dc:date>2008-11-18T15:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62021#M13489</link>
      <description>It seem that you have borrowed some syntax from an other programming language (in your calculations for example). You said that you calculated Maxcount, how? If you pre-calculated, is it in every row of work1 table? A more convenient way is to store it in a macro variable.&lt;BR /&gt;
&lt;BR /&gt;
Please attach some sample input data, and desired output, and maybe describe what a bucket is in this example.&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Linus</description>
      <pubDate>Tue, 18 Nov 2008 20:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62021#M13489</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-11-18T20:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62022#M13490</link>
      <description>As Linus said: You will have to specify a bit more what you have and what you want to achieve in order to make us understand.&lt;BR /&gt;
&lt;BR /&gt;
Just some ideas:&lt;BR /&gt;
Have a look at the array statement, Proc Transpose, Proc Format and may be hash tables - Proc Format and Hash Tables would be used for table lookup.&lt;BR /&gt;
&lt;BR /&gt;
Cheers, Patrick</description>
      <pubDate>Wed, 19 Nov 2008 11:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62022#M13490</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2008-11-19T11:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62023#M13491</link>
      <description>Here is the code that gives me MaxCount:&lt;BR /&gt;
&lt;BR /&gt;
data work;&lt;BR /&gt;
set rec22;&lt;BR /&gt;
retain polno1 count1;&lt;BR /&gt;
if polno ~= polno1 then&lt;BR /&gt;
count =1;&lt;BR /&gt;
else if polno = polno1 then count=count1+1;&lt;BR /&gt;
polno1= polno;&lt;BR /&gt;
count1 =count;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Proc Sort data=work;&lt;BR /&gt;
by descending Count;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Data work1;&lt;BR /&gt;
set work;&lt;BR /&gt;
retain Maxcount;&lt;BR /&gt;
Maxcount = Max(Maxcount, count);&lt;BR /&gt;
run;

Message was edited by: Chiefsfan4ever</description>
      <pubDate>Wed, 19 Nov 2008 15:17:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62023#M13491</guid>
      <dc:creator>Chiefsfan4ever</dc:creator>
      <dc:date>2008-11-19T15:17:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62024#M13492</link>
      <description>I'm not sure if I'm clarifying what my objective is here.  Let me give an example:&lt;BR /&gt;
&lt;BR /&gt;
Policy   Bucket1 Bucket2 Bucket3 Bucket4 Bucket5 Count MaxCount&lt;BR /&gt;
1              10         20            30      40          50          1          2&lt;BR /&gt;
1              30          0              0        0            0          2          2&lt;BR /&gt;
2              10          20            0        0            0          1         1&lt;BR /&gt;
&lt;BR /&gt;
I would like the information to be displayed in the dataset this way:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Policy   Bucket1 Bucket2 Bucket3 Bucket4 Bucket5 Bucket6&lt;BR /&gt;
1              10         20            30      40          50          30&lt;BR /&gt;
2              10          20            0        0            0           0&lt;BR /&gt;
&lt;BR /&gt;
I don't know how many rows there will be so it could go out to Bucket100 or Bucket10000000 or what not.  Hopefully this information will be helpful.</description>
      <pubDate>Wed, 19 Nov 2008 17:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62024#M13492</guid>
      <dc:creator>Chiefsfan4ever</dc:creator>
      <dc:date>2008-11-19T17:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62025#M13493</link>
      <description>To avoid some heavy data step programming, I suggest that you first transpose the data downwards, so that you will have one roe per policy and bucket. Then you can us PROC TRANSPOSE to transpose your data upwards again. I think this is quite nice since you don't have to keep track of "anything", like maxcout etc:&lt;BR /&gt;
&lt;BR /&gt;
data transpose1;&lt;BR /&gt;
	set policies;&lt;BR /&gt;
	array buckets[5] bucket1-bucket5;&lt;BR /&gt;
	do i = 1 to 5;&lt;BR /&gt;
		if buckets&lt;I&gt; ne 0 then do;&lt;BR /&gt;
			bucket = buckets&lt;I&gt;;&lt;BR /&gt;
			output;&lt;BR /&gt;
		end;&lt;BR /&gt;
	end;&lt;BR /&gt;
	keep policy bucket;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=transpose1 out=transpose2(drop=_name_) prefix=bucket;&lt;BR /&gt;
	by policy;&lt;BR /&gt;
	var bucket;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Hope this applies to your problem.&lt;BR /&gt;
&lt;BR /&gt;
/Linus&lt;/I&gt;&lt;/I&gt;</description>
      <pubDate>Wed, 19 Nov 2008 19:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62025#M13493</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-11-19T19:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62026#M13494</link>
      <description>Hey Linus,&lt;BR /&gt;
&lt;BR /&gt;
I'm getting an error with this code.  What am I missing?&lt;BR /&gt;
&lt;BR /&gt;
335  if buckets ne 0 then do;&lt;BR /&gt;
ERROR: Illegal reference to the array buckets.&lt;BR /&gt;
336  bucket = buckets;&lt;BR /&gt;
ERROR: Illegal reference to the array buckets.</description>
      <pubDate>Thu, 20 Nov 2008 17:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62026#M13494</guid>
      <dc:creator>Chiefsfan4ever</dc:creator>
      <dc:date>2008-11-20T17:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62027#M13495</link>
      <description>It seems that the forum application didn't like my brackets in the code, sorry I didn't notice that. Let's try the same code with ordinary parenthesis:&lt;BR /&gt;
&lt;BR /&gt;
data transpose1;&lt;BR /&gt;
	set policies;&lt;BR /&gt;
	array buckets(5) bucket1-bucket5;&lt;BR /&gt;
	do i = 1 to 5;&lt;BR /&gt;
		if buckets(i) ne 0 then do;&lt;BR /&gt;
			bucket = buckets(i);&lt;BR /&gt;
			output;&lt;BR /&gt;
		end;&lt;BR /&gt;
	end;&lt;BR /&gt;
	keep policy bucket;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Thu, 20 Nov 2008 17:23:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62027#M13495</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-11-20T17:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an infinite number of variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62028#M13496</link>
      <description>Perfect Linus.  Thank you so much.  If you were here, I'd give you a hug.</description>
      <pubDate>Thu, 20 Nov 2008 18:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-infinite-number-of-variables-in-a-loop/m-p/62028#M13496</guid>
      <dc:creator>Chiefsfan4ever</dc:creator>
      <dc:date>2008-11-20T18:58:40Z</dc:date>
    </item>
  </channel>
</rss>

