<?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: Do-loop question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384179#M91699</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Linmuxi wrote: &lt;BR /&gt;
&lt;P&gt;Since my real dataset is really large(about 6000 accounts and 50,000 records),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For SAS, 50.000 records is (almost) negligibly close to nothing. Standard data/sort/SQL steps will take considerably less than a second.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do account = 1 to 6000;
  do month = 1 to 12;
    balance = int(rand('uniform') * 1000);
    delin_day = round(rand('uniform'));
    output;
  end;
end;
run;

data want (keep=month balance);
set have end=done;
array m {12} m1-m12;
if delin_day &amp;gt; 0 then m{month} + balance;
if done
then do month = 1 to 12;
  balance = m{month};
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first step creates a random-filled dataset like the one you have, with 72K records. The next one is &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s suggested solution.&lt;/P&gt;
&lt;P&gt;This is the log:&lt;/P&gt;
&lt;PRE&gt;41         data have;
42         do account = 1 to 6000;
43           do month = 1 to 12;
44             balance = int(rand('uniform') * 1000);
45             delin_day = round(rand('uniform'));
46             output;
47           end;
48         end;
49         run;

NOTE: The data set WORK.HAVE has 72000 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.02 seconds

50         
51         data want (keep=month balance);
52         set have end=done;
53         array m {12} m1-m12;
54         if delin_day &amp;gt; 0 then m{month} + balance;
55         if done
56         then do month = 1 to 12;
57           balance = m{month};
58           output;
59         end;
60         run;

NOTE: There were 72000 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 12 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;(run on a 2-core pSeries)&lt;/P&gt;
&lt;P&gt;You see that the total time for creating and analysing the data is 0.05 seconds (!).&lt;/P&gt;
&lt;P&gt;Even if your 50k dataset has lots of columns, you'll stay in the seconds range.&lt;/P&gt;</description>
    <pubDate>Mon, 31 Jul 2017 08:27:23 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-07-31T08:27:23Z</dc:date>
    <item>
      <title>Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384114#M91667</link>
      <description>&lt;P&gt;I have a dataset like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ACCT&lt;/TD&gt;&lt;TD&gt;Month&lt;/TD&gt;&lt;TD&gt;Delin_day&lt;/TD&gt;&lt;TD&gt;Balance&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;600&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am trying to do is to calculate the sum of delinquency balance for each month. So we should caculate the sum of balance where Delin_day &amp;gt;0 for each month. For example, for Month 1, the Sum shold be 0 &amp;nbsp;since no account shows delinquency. For month 2 ,the Sum should be 600 since the account 2 and account3 are both delinquency.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since my real dataset is really large(about 6000 accounts and 50,000 records), I am trying to use do loop to complete it. But my do-loop doesn't work. &amp;nbsp;Can anyone help me with this please...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BTW, the max months in the dataset is 12, if it's useful. Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jul 2017 22:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384114#M91667</guid>
      <dc:creator>Linmuxi</dc:creator>
      <dc:date>2017-07-30T22:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384116#M91668</link>
      <description>&lt;P&gt;You could do this with PROC SORT and PROC MEANS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length acct $ 5 month $2;
  infile datalines dlm='09'x missover;
  input acct month delin_day balance;
  datalines;
1	1	0	100
1	2	0	200
1	3	4	200
1	4	2	300
2	1	0	600
2	2	1	400
2	3	0	300
3	1	0	300
3	2	5	200
3	3	0	200
3	4	4	300
;
run;

proc sort data=have;
  by month;
run;

proc means data=have sum noprint;
  where delin_day gt 0;
  var balance;
  by month;
  output out=want sum=delinquent_balance;
run;

proc print data=want noobs;
  var month delinquent_balance;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Jul 2017 23:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384116#M91668</guid>
      <dc:creator>SuzanneDorinski</dc:creator>
      <dc:date>2017-07-30T23:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384117#M91669</link>
      <description>&lt;P&gt;Show your full input and expected output.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;YOU DO NOT USE A DO LOOP TO LOOP THROUGH OBSERVATIONS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS data step loops automatically. In SAS, a DO LOOP is used to loop through variables in a row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, there are exceptions to the above comment, but since I think you're a beginner I wouldn't go down that route at all right now. Honestly, I'm still not good at using DO LOOPS that do this and I've been programming with SAS for over a decade.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on what exactly you need, ie a running total or sum then PROC EXPAND may be an option, or a simple data step may suffice.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jul 2017 23:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384117#M91669</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-30T23:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384119#M91670</link>
      <description>&lt;P&gt;Hi, The answer has been provided to you with proc means. However, I am just curious to understand what you meant by using a do-loop when the objective seems like to merely summarize. Did you mean a one full pass of the dataset as one implicit loop without a "do statement" or you wanted to experiment Ian's DOW-do until(last.by_variable)&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jul 2017 23:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384119#M91670</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-07-30T23:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384121#M91671</link>
      <description>&lt;P&gt;With 50K observations you'd spend more time implementing a DoW loop that you would get back ever from a possible increased efficiency in run time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a perfectly valid solution, but when searching for an answer I try to balance efficiency of code and my time. &amp;nbsp;I tend to lean towards my time, since that's more of an issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="automation" style="width: 404px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14219i5F45A10139CD886C/image-size/large?v=v2&amp;amp;px=999" role="button" title="automation" alt="automation" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://imgs.xkcd.com/comics/automation.png" target="_self"&gt;https://imgs.xkcd.com/comics/automation.png&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jul 2017 23:33:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384121#M91671</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-30T23:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384129#M91672</link>
      <description>&lt;P&gt;By most standards, your data set isn't really large. &amp;nbsp;But if it were actually 1000 times larger, you might consider a process that would only take one step instead of two:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have end=done;&lt;/P&gt;
&lt;P&gt;array m {12} m1-m12;&lt;/P&gt;
&lt;P&gt;if delin_day &amp;gt; 0 then m{month} + balance;&lt;/P&gt;
&lt;P&gt;if done;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At this point, you have a couple of choices &amp;nbsp;You could simply add:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;keep m1-m12;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That would give you a single observation, with total delinquent balances for each month. &amp;nbsp;Alternatively, you could transform the data as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do month = 1 to 12;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;delin_balance = m{month};&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep month delin_balance;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That would give you 12 observations, a separate observation for each month.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jul 2017 00:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384129#M91672</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-31T00:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384179#M91699</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Linmuxi wrote: &lt;BR /&gt;
&lt;P&gt;Since my real dataset is really large(about 6000 accounts and 50,000 records),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For SAS, 50.000 records is (almost) negligibly close to nothing. Standard data/sort/SQL steps will take considerably less than a second.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do account = 1 to 6000;
  do month = 1 to 12;
    balance = int(rand('uniform') * 1000);
    delin_day = round(rand('uniform'));
    output;
  end;
end;
run;

data want (keep=month balance);
set have end=done;
array m {12} m1-m12;
if delin_day &amp;gt; 0 then m{month} + balance;
if done
then do month = 1 to 12;
  balance = m{month};
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first step creates a random-filled dataset like the one you have, with 72K records. The next one is &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s suggested solution.&lt;/P&gt;
&lt;P&gt;This is the log:&lt;/P&gt;
&lt;PRE&gt;41         data have;
42         do account = 1 to 6000;
43           do month = 1 to 12;
44             balance = int(rand('uniform') * 1000);
45             delin_day = round(rand('uniform'));
46             output;
47           end;
48         end;
49         run;

NOTE: The data set WORK.HAVE has 72000 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.02 seconds

50         
51         data want (keep=month balance);
52         set have end=done;
53         array m {12} m1-m12;
54         if delin_day &amp;gt; 0 then m{month} + balance;
55         if done
56         then do month = 1 to 12;
57           balance = m{month};
58           output;
59         end;
60         run;

NOTE: There were 72000 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 12 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;(run on a 2-core pSeries)&lt;/P&gt;
&lt;P&gt;You see that the total time for creating and analysing the data is 0.05 seconds (!).&lt;/P&gt;
&lt;P&gt;Even if your 50k dataset has lots of columns, you'll stay in the seconds range.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jul 2017 08:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384179#M91699</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-31T08:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384495#M91823</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12496"&gt;@SuzanneDorinski&lt;/a&gt;&amp;nbsp;It is much better to not sort before proc means and use a class statement instead. This improves both legibility (less code) and speed (less processing -sorts are very expensive-).&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 04:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384495#M91823</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-08-01T04:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Do-loop question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384525#M91836</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12496"&gt;@SuzanneDorinski&lt;/a&gt;&amp;nbsp;It is much better to not sort before proc means and use a class statement instead. This improves both legibility (less code) and speed (less processing -sorts are very expensive-).&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But be aware that proc means may fail with memory problems if the variables in the class statement have high cardinality.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 06:27:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-question/m-p/384525#M91836</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-01T06:27:30Z</dc:date>
    </item>
  </channel>
</rss>

