<?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: How to use do loop to calculate multiple cols in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925999#M364381</link>
    <description>&lt;P&gt;Thanks so much for the prompt reply! I did notice the variable name issue and updated sample data codes. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please pardon me as the following question might sounds silly:&lt;/P&gt;
&lt;P&gt;I am new to do loops. I thought when we defined 'do i=1 to 3' , and then when we call variable name _den&lt;U&gt;&lt;STRONG&gt;i,&lt;/STRONG&gt;&lt;/U&gt; it will auto updated to _den1, _den2, _den3 in the looping process. Did I misunderstand it? if it is incorrect, is it possible to create/calculate such variables in a do loop codes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
    <pubDate>Fri, 26 Apr 2024 13:00:56 GMT</pubDate>
    <dc:creator>stataq</dc:creator>
    <dc:date>2024-04-26T13:00:56Z</dc:date>
    <item>
      <title>How to use do loop to calculate multiple cols</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925991#M364377</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset 'ds' and I would like to calculate 95CI using&amp;nbsp; _denX and _numX. There are 3 set of _den and _num. I would like to use do loop to calculate them all at once. I am new to sas and tried but failed. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt; Could anyone guide me on this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If do loop is not the right way, could you show me the right way? I would like to know as many approach as possible.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;PRE&gt;/*Sample datset*/
data ds;
infile datalines dsd truncover;
input id _den1 _den2 _den3 _num1 _num2  _num3;
datalines;
1,4,7,6,0,3,2,
2,4,7,6,1,0,3,
3,4,7,6,0,2,1,
4,4,7,6,2,1,0
;

/*Looping code. Failed.*/
data want;
		set ds;
		do i=1 to 3;
				_pi = round((_numi/_deni),.0001);
				if _pi=0 then _lowi=0;
				if _pi=1 then _highi=100;
				if _ pi ne 0 then _lowi=round((1-betainv(.975,(_deni-_numi+1),_numi)),.0001)*100;
				if _pi ne 1 then _highi=round((1-betainv(.025,(_deni-_numi),_numi+1)),.0001)*100;
				resulti = '['||strip(put(_lowi, 5.1))||', '||strip(put(_highi, 5.1))||']';
		end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2024 12:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925991#M364377</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-04-26T12:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to use do loop to calculate multiple cols</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925995#M364379</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your code also looks for a variable named deni and numi, neither of those exist in the data set. You have to use variable names that exist in the data set, otherwise the code will fail.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems like a job for an ARRAY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds;
infile datalines dsd truncover;
input id _den1 _den2 _den3 _cnt1 _cnt2  _cnt3;
datalines;
1,4,7,6,0,3,2,
2,4,7,6,1,0,3,
3,4,7,6,0,2,1,
4,4,7,6,2,1,0
;

data want;
	set ds;
    array n _cnt1-_cnt3;
    array d _den1-_den3;
    array low _low1-_low3;
    array h _high1-_high3;
    array r $16 result1-result3;
	do i=1 to dim(n);
		_pi = round((n(i)/d(i)),.0001);
		if _pi=0 then low(i)=0;
		if _pi=1 then h(i)=100;
		if _pi ne 0 then low(i)=round((1-betainv(.975,(d(i)-n(i)+1),d(i))),.0001)*100;
		if _pi ne 1 then h(i)=round((1-betainv(.025,(d(i)-n(i)),n(i)+1)),.0001)*100;
		r(i)= '['||strip(put(low(i), 5.1))||', '||strip(put(h(i), 5.1))||']';
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code could be further simplified, but I will leave it as is. Also, wide data sets are usually not preferred, if this was a long data set, no arrays would be needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would help tremendously if you would use the same variable names in your text as in your data set. There is no _numX variable in your data set. I assume you mean _cntX.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 12:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925995#M364379</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-26T12:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to use do loop to calculate multiple cols</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925998#M364380</link>
      <description>&lt;P&gt;You did not include any ARRAY statements in your data step.&amp;nbsp; Read the documentation on how arrays work.&lt;/P&gt;
&lt;P&gt;So perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds;
  input id _den1-_den3 _num1-_num3;
datalines;
1 4 7 6 0 3 2 
2 4 7 6 1 0 3 
3 4 7 6 0 2 1 
4 4 7 6 2 1 0
;

data want;
  set ds;
  array _den _den1-_den3;
  array _num _num1-_num3;
  array _low _low1-_low3;
  array _high _high1-_high3;
  array result $15 result1-result3;
  do i=1 to dim(_den);
    _pi = round((_num[i]/_den[i]),.0001);
    if _pi=0 then _low[i]=0;
    else _low[i]=round((1-betainv(.975,(_den[i]-_num[i]+1),_num[i])),.0001)*100;
    if _pi=1 then _high[i]=100;
    else _high[i]=round((1-betainv(.025,(_den[i]-_num[i]),_num[i]+1)),.0001)*100;
    result[i] = cats('[',put(_low[i], 5.1),',',put(_high[i], 5.1),']');
  end;
  drop i _pi ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1714136232268.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95922iC4EACF50D6288263/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1714136232268.png" alt="Tom_0-1714136232268.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 13:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925998#M364380</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-26T13:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to use do loop to calculate multiple cols</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925999#M364381</link>
      <description>&lt;P&gt;Thanks so much for the prompt reply! I did notice the variable name issue and updated sample data codes. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please pardon me as the following question might sounds silly:&lt;/P&gt;
&lt;P&gt;I am new to do loops. I thought when we defined 'do i=1 to 3' , and then when we call variable name _den&lt;U&gt;&lt;STRONG&gt;i,&lt;/STRONG&gt;&lt;/U&gt; it will auto updated to _den1, _den2, _den3 in the looping process. Did I misunderstand it? if it is incorrect, is it possible to create/calculate such variables in a do loop codes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 13:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/925999#M364381</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-04-26T13:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to use do loop to calculate multiple cols</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/926001#M364382</link>
      <description>&lt;P&gt;Do loops work fine.&amp;nbsp; So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to 3;
   put "Current value of I is ' i ;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;works fine.&lt;/P&gt;
&lt;P&gt;But your mistake is trying to reference other variables.&amp;nbsp; If you code something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;i=2;
deni=45;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have created two variables. One name i and one named DENi.&amp;nbsp; You have not made any attempt to reference a variable named DEN2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 13:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/926001#M364382</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-26T13:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to use do loop to calculate multiple cols</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/926030#M364399</link>
      <description>&lt;P&gt;_deni is the name of a variable, not an array element. There is no variable by that name in your data set. _highi is the name of a variable, not an array element.&amp;nbsp;There is no variable by that name in your data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you have an ARRAY statement and refer to h(i), this means the i-th variable of array H, which is one of several arrays I defined, containing the _HIGH variable.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 14:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-do-loop-to-calculate-multiple-cols/m-p/926030#M364399</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-26T14:55:09Z</dc:date>
    </item>
  </channel>
</rss>

