<?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 macro do loop help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75814#M16341</link>
    <description>How can I use value of a variable within a dataset to create a macro do loop? My objective is to create a do loop within a data step where the end value for the do loop changes per row. For example, in the code below, I want to use upperlimit in the do loop.&lt;BR /&gt;
&lt;BR /&gt;
%macro testing();&lt;BR /&gt;
data test;&lt;BR /&gt;
set mydata;&lt;BR /&gt;
counter = 0;&lt;BR /&gt;
upperlimit = ranuni(3271985);&lt;BR /&gt;
%do i = 1 %to upperlimit;&lt;BR /&gt;
    counter = counter + 1;&lt;BR /&gt;
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%testing;</description>
    <pubDate>Tue, 20 Apr 2010 20:02:04 GMT</pubDate>
    <dc:creator>model_coder</dc:creator>
    <dc:date>2010-04-20T20:02:04Z</dc:date>
    <item>
      <title>macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75814#M16341</link>
      <description>How can I use value of a variable within a dataset to create a macro do loop? My objective is to create a do loop within a data step where the end value for the do loop changes per row. For example, in the code below, I want to use upperlimit in the do loop.&lt;BR /&gt;
&lt;BR /&gt;
%macro testing();&lt;BR /&gt;
data test;&lt;BR /&gt;
set mydata;&lt;BR /&gt;
counter = 0;&lt;BR /&gt;
upperlimit = ranuni(3271985);&lt;BR /&gt;
%do i = 1 %to upperlimit;&lt;BR /&gt;
    counter = counter + 1;&lt;BR /&gt;
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%testing;</description>
      <pubDate>Tue, 20 Apr 2010 20:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75814#M16341</guid>
      <dc:creator>model_coder</dc:creator>
      <dc:date>2010-04-20T20:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75815#M16342</link>
      <description>Is there a reason why you need to use a macro with %do instead of using the regular data step do loop?  Also, ranuni gives a value from 0 to 1 so you probably want to change that.</description>
      <pubDate>Tue, 20 Apr 2010 20:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75815#M16342</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2010-04-20T20:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75816#M16343</link>
      <description>Rick,&lt;BR /&gt;
&lt;BR /&gt;
I just used ranuni as an example. Actually in my code, I have column names that are numbered from 1 to 10. For example, col_1, col_2, col_3..col_10. For each row, we determine how many columns should feed into the sum. So for example, for row 1, we need to add 4 columns to compute a sum. Similarly, for second row, we may use 5 columns to calculate sum. I have updated the code to make logic little more cleaner.&lt;BR /&gt;
&lt;BR /&gt;
%macro testing();&lt;BR /&gt;
data test;&lt;BR /&gt;
set mydata;&lt;BR /&gt;
sum = 0;&lt;BR /&gt;
column_count = func(some_logic);&lt;BR /&gt;
%do i = 1 %to column_count ;&lt;BR /&gt;
sum=sum+col_&amp;amp;i;&lt;BR /&gt;
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%testing;

Message was edited by: model_coder</description>
      <pubDate>Tue, 20 Apr 2010 20:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75816#M16343</guid>
      <dc:creator>model_coder</dc:creator>
      <dc:date>2010-04-20T20:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75817#M16344</link>
      <description>Hi:&lt;BR /&gt;
  I agree with Rick. I'm not sure you need a do loop at all -- not a Macro %DO loop or a DATA step DO loop. &lt;BR /&gt;
&lt;BR /&gt;
  Consider the following program. It uses the SUM function to create the SUM1 variable, then uses the NMISS and N functions to see which of the 10 COL_x variables is missing or not.&lt;BR /&gt;
&lt;BR /&gt;
  As an alternative a DATA Step array is used with a regular DO loop to calculate the SUM2 variable. As you can see in the output, the result for SUM1 and SUM2 is the same.&lt;BR /&gt;
&lt;BR /&gt;
  If you need to know how many columns are "filled" and how many are "empty" or missing, the other SAS functions do that quite well -- see how the values for NUM_MISSING and NUM_THERE were calculated.&lt;BR /&gt;
  &lt;BR /&gt;
  Here's the program and output.&lt;BR /&gt;
&lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** make some fake data;&lt;BR /&gt;
data testdata;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input cat $ col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9 col_10;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
A 1 1 1 1 1 1 1 1 1 .&lt;BR /&gt;
B 2 2 2 2 2 2 2 2 2 2&lt;BR /&gt;
C 1 1 1 1 1 . . . . .&lt;BR /&gt;
D 2 2 . . . . . . . .&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                              &lt;BR /&gt;
data calcsum;&lt;BR /&gt;
   set testdata;&lt;BR /&gt;
   array sd col_1-col_10;&lt;BR /&gt;
                            &lt;BR /&gt;
   ** Approach 1: Use SUM function, which ignores missing values;&lt;BR /&gt;
   sum1 = sum (of col_1-col_10);&lt;BR /&gt;
   num_missing = nmiss(of col_1-col_10);&lt;BR /&gt;
   num_there = n(of col_1-col_10);&lt;BR /&gt;
                   &lt;BR /&gt;
   ** Approach 2: use ARRAY  with num_there variable value;&lt;BR /&gt;
   ** to avoid missing values in calculation of SUM2 variable.;&lt;BR /&gt;
   sum2 = 0;&lt;BR /&gt;
   do i = 1 to num_there;&lt;BR /&gt;
      sum2 = sum2 + sd(i);&lt;BR /&gt;
   end;&lt;BR /&gt;
   label sum1 = 'Calc with SUM function'&lt;BR /&gt;
         sum2 = 'Calc with DATA step array';&lt;BR /&gt;
run;&lt;BR /&gt;
                                     &lt;BR /&gt;
options nodate ls=120;&lt;BR /&gt;
proc print data=calcsum label;&lt;BR /&gt;
  title 'Probably do not need an array for SUM2';&lt;BR /&gt;
  var cat col: num_missing num_there sum1 sum2;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
 &lt;BR /&gt;
and here's the output:&lt;BR /&gt;
[pre]&lt;BR /&gt;
                                                                                                  Calc with  Calc with&lt;BR /&gt;
                                                                                    num_    num_     SUM     DATA step&lt;BR /&gt;
 Obs  cat  col_1  col_2  col_3  col_4  col_5  col_6  col_7  col_8  col_9  col_10  missing  there   function    array&lt;BR /&gt;
&lt;BR /&gt;
  1    A     1      1      1      1      1      1      1      1      1       .       1        9        9          9&lt;BR /&gt;
  2    B     2      2      2      2      2      2      2      2      2       2       0       10       20         20&lt;BR /&gt;
  3    C     1      1      1      1      1      .      .      .      .       .       5        5        5          5&lt;BR /&gt;
  4    D     2      2      .      .      .      .      .      .      .       .       8        2        4          4&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 20 Apr 2010 20:46:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75817#M16344</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-04-20T20:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75818#M16345</link>
      <description>Thanks Cynthia. I made it work with array.

Message was edited by: model_coder</description>
      <pubDate>Tue, 20 Apr 2010 21:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-help/m-p/75818#M16345</guid>
      <dc:creator>model_coder</dc:creator>
      <dc:date>2010-04-20T21:08:01Z</dc:date>
    </item>
  </channel>
</rss>

