<?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: Converting a  column into an Array for nested do loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575225#M162662</link>
    <description>&lt;P&gt;You could also use data steps and the automatic loop to get things done. Given your current code, where you never use a, you can use this approach. I'm not sure what the value of having information in the log is via PUT statements versus having that stored in a data set that's much easier to query.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input temp;
cards;
200
203
205
206
208
250 
300
;
run;

data want;
set have end=eof;
retain cum_total;
if _n_ &amp;lt;= 5 then cum_total = sum(cum_total, temp);
if eof and cum_total &amp;gt; 300 then alert="Generated" ; 
else if eof then alert="Not Generated";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282259"&gt;@Kirotheninja&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE class="_3rGPyS-T4z661D_PbTU36A allowTextSelection"&gt;data alert;
ARRAY temp[7] (200 203 205 206 208 250 300);
put _all_;
/*Running nested do loops over the array*/
sum1=0;
do i=1 to dim(temp);
	c=0;sum2=0;
		do j=1 to dim(temp);
		put temp[i]=;
		put temp[j]=;
			a=abs((temp[i]-temp[j])/temp[i])*100;
			if a&amp;lt;5 then sum2=sum2+temp[j];
			put a=;
		c=sum2;
	if c&amp;gt;sum1 then sum1=c;
	put sum2=;
	put sum1=;
	end;
	end;
	drop i j  sum2 a c temp1-temp7;
/*Check with the threshold for AGP*/
threshold=300;
if sum1&amp;gt;threshold then alert="Generated" ; else alert="Not Generated";
run;
proc print;
run;&lt;/PRE&gt;
&lt;P&gt;This is my intention. But here i'm having to put in the values into an array manually. is tthere any way i can reference these values automatically?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 21 Jul 2019 20:23:00 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-07-21T20:23:00Z</dc:date>
    <item>
      <title>Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575177#M162637</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cash_flow;
input account_num 
	  account_desc $
	  trans_key
	  amount
	  tran_status $
	  ;
cards;
1 P 23 200 SUCCESS
2 P 45 205 SUCCESS
3 P 22 409 FAILURE
4 P 34 343 SUCCESS
1 P 23 343 SUCCESS
1 P 45 506 SUCCESS
;
RUN;
/* --------------------------------------------------------------------
Creating a table with required parameters
   -------------------------------------------------------------------- */
PROC SQl;
create table dat1 as
select * from cash_flow
where account_desc='P' and tran_status='SUCCESS' and account_num=1;
quit;
proc sql;
create table want as 
select amount from dat1;
data arraysss;
set want;
array try[*] _numeric_;
sum1=0;
do i=1 to dim(try);
	c=0;sum2=0;
		do j=1 to dim(try);
		put try[i]=;
		put try[j]=;
			a=abs((try[i]-try[j])/try[i])*100;
			if a&amp;lt;5 then sum2=sum2+try[j];
			put a=;
		c=sum2;
	if c&amp;gt;sum1 then sum1=c;

	end;
	end;
	drop i j;
threshold=100;
if sum1&amp;gt;threshold then alert=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can you tell me where i'm going wrong. If i write the values into the array manually, the loop seems to work just fine. but when i'm needed for the array to be as the data provided in the above table i haven't been able to &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 04:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575177#M162637</guid>
      <dc:creator>Kirotheninja</dc:creator>
      <dc:date>2019-07-21T04:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575180#M162638</link>
      <description>&lt;P&gt;It will be easy to check the code if you can show the expected output.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 05:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575180#M162638</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-21T05:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575183#M162639</link>
      <description>&lt;P&gt;Table want has only one column, so the array will have a size of one and the loops only one iteration.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 05:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575183#M162639</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-21T05:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575206#M162648</link>
      <description>&lt;P&gt;Please explain what you are trying to do.&lt;/P&gt;
&lt;P&gt;Since your dataset WANT only has one variable the ARRAY will only have one variable in it.&amp;nbsp; So there is nothing to loop over.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So on the first observation the variable AMOUNT has the value 200.&lt;/P&gt;
&lt;P&gt;The array has a dimension of 1.&lt;/P&gt;
&lt;P&gt;So your code set these values:&lt;/P&gt;
&lt;PRE&gt;sum1=0
c=0
sum2=0
a=0
sum2=200
c=200
sum1=200
threshold=100
alert=1&lt;/PRE&gt;
&lt;P&gt;So at the end the first iteration of the data step the values will be.&lt;/P&gt;
&lt;PRE&gt;amount=200 sum1=200 c=200 sum2=200 a=0 threshol=100 alert=1&lt;/PRE&gt;
&lt;P&gt;So that observation is written and the data step will now process the next observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 15:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575206#M162648</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-21T15:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575208#M162650</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data alert;
ARRAY temp[7] (200 203 205 206 208 250 300);
put _all_;
/*Running nested do loops over the array*/
sum1=0;
do i=1 to dim(temp);
	c=0;sum2=0;
		do j=1 to dim(temp);
		put temp[i]=;
		put temp[j]=;
			a=abs((temp[i]-temp[j])/temp[i])*100;
			if a&amp;lt;5 then sum2=sum2+temp[j];
			put a=;
		c=sum2;
	if c&amp;gt;sum1 then sum1=c;
	put sum2=;
	put sum1=;
	end;
	end;
	drop i j  sum2 a c temp1-temp7;
/*Check with the threshold for AGP*/
threshold=300;
if sum1&amp;gt;threshold then alert="Generated" ; else alert="Not Generated";
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;this runs perfectly as intended. please check&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 16:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575208#M162650</guid>
      <dc:creator>Kirotheninja</dc:creator>
      <dc:date>2019-07-21T16:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575209#M162651</link>
      <description>&lt;P&gt;yeah but i want to run a nested loop. like in this code shown below.&lt;/P&gt;&lt;PRE class="_3rGPyS-T4z661D_PbTU36A allowTextSelection"&gt;data alert;
ARRAY temp[7] (200 203 205 206 208 250 300);
put _all_;
/*Running nested do loops over the array*/
sum1=0;
do i=1 to dim(temp);
	c=0;sum2=0;
		do j=1 to dim(temp);
		put temp[i]=;
		put temp[j]=;
			a=abs((temp[i]-temp[j])/temp[i])*100;
			if a&amp;lt;5 then sum2=sum2+temp[j];
			put a=;
		c=sum2;
	if c&amp;gt;sum1 then sum1=c;
	put sum2=;
	put sum1=;
	end;
	end;
	drop i j  sum2 a c temp1-temp7;
/*Check with the threshold for AGP*/
threshold=300;
if sum1&amp;gt;threshold then alert="Generated" ; else alert="Not Generated";
run;
proc print;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Jul 2019 16:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575209#M162651</guid>
      <dc:creator>Kirotheninja</dc:creator>
      <dc:date>2019-07-21T16:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575210#M162652</link>
      <description>&lt;PRE class="_3rGPyS-T4z661D_PbTU36A allowTextSelection"&gt;data alert;
ARRAY temp[7] (200 203 205 206 208 250 300);
put _all_;
/*Running nested do loops over the array*/
sum1=0;
do i=1 to dim(temp);
	c=0;sum2=0;
		do j=1 to dim(temp);
		put temp[i]=;
		put temp[j]=;
			a=abs((temp[i]-temp[j])/temp[i])*100;
			if a&amp;lt;5 then sum2=sum2+temp[j];
			put a=;
		c=sum2;
	if c&amp;gt;sum1 then sum1=c;
	put sum2=;
	put sum1=;
	end;
	end;
	drop i j  sum2 a c temp1-temp7;
/*Check with the threshold for AGP*/
threshold=300;
if sum1&amp;gt;threshold then alert="Generated" ; else alert="Not Generated";
run;
proc print;
run;&lt;/PRE&gt;&lt;P&gt;This is my intention. But here i'm having to put in the values into an array manually. is tthere any way i can reference these values automatically?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 16:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575210#M162652</guid>
      <dc:creator>Kirotheninja</dc:creator>
      <dc:date>2019-07-21T16:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575211#M162653</link>
      <description>&lt;P&gt;Explain what you are trying to do and perhaps someone can translate into code that can work on&amp;nbsp; dataset instead of just a single list of numbers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 16:50:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575211#M162653</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-21T16:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575212#M162654</link>
      <description>&lt;P&gt;It is trivial to take all of the observations for a single variable and rotate it up into many variables on one observation.&lt;/P&gt;
&lt;P&gt;You could use proc transpose.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want;
  var amount ;
run;
data my_calculations;
  set want;
  array temp col: ;
  ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could just load your array in your data step. Just make the array larger than you should ever need and use the actual number of values instead of the array dimension as the upper bound in your loops.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_calculations;
  array temp [10000] _temporary_;
  if _n_=1 then do i=1 to nobs;
    set have nobs=nobs;
    temp[i]=amount;
  end;
  ....
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 16:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575212#M162654</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-21T16:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575225#M162662</link>
      <description>&lt;P&gt;You could also use data steps and the automatic loop to get things done. Given your current code, where you never use a, you can use this approach. I'm not sure what the value of having information in the log is via PUT statements versus having that stored in a data set that's much easier to query.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input temp;
cards;
200
203
205
206
208
250 
300
;
run;

data want;
set have end=eof;
retain cum_total;
if _n_ &amp;lt;= 5 then cum_total = sum(cum_total, temp);
if eof and cum_total &amp;gt; 300 then alert="Generated" ; 
else if eof then alert="Not Generated";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282259"&gt;@Kirotheninja&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE class="_3rGPyS-T4z661D_PbTU36A allowTextSelection"&gt;data alert;
ARRAY temp[7] (200 203 205 206 208 250 300);
put _all_;
/*Running nested do loops over the array*/
sum1=0;
do i=1 to dim(temp);
	c=0;sum2=0;
		do j=1 to dim(temp);
		put temp[i]=;
		put temp[j]=;
			a=abs((temp[i]-temp[j])/temp[i])*100;
			if a&amp;lt;5 then sum2=sum2+temp[j];
			put a=;
		c=sum2;
	if c&amp;gt;sum1 then sum1=c;
	put sum2=;
	put sum1=;
	end;
	end;
	drop i j  sum2 a c temp1-temp7;
/*Check with the threshold for AGP*/
threshold=300;
if sum1&amp;gt;threshold then alert="Generated" ; else alert="Not Generated";
run;
proc print;
run;&lt;/PRE&gt;
&lt;P&gt;This is my intention. But here i'm having to put in the values into an array manually. is tthere any way i can reference these values automatically?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2019 20:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575225#M162662</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-21T20:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a  column into an Array for nested do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575278#M162696</link>
      <description>&lt;P&gt;Thanks a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 06:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-a-column-into-an-Array-for-nested-do-loops/m-p/575278#M162696</guid>
      <dc:creator>Kirotheninja</dc:creator>
      <dc:date>2019-07-22T06:47:31Z</dc:date>
    </item>
  </channel>
</rss>

