<?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: iterative Do loops. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823576#M325193</link>
    <description>&lt;P&gt;Thank you so much for your fast response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I whant to repeat more than 12 times for exemple n times. I could replace n when I would like by 12 or 20 for example. So, the method with an array doesn't work, when you do know the dimension of array?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have you got another way, please? I I would like generalize to n times&lt;/P&gt;</description>
    <pubDate>Fri, 15 Jul 2022 20:13:19 GMT</pubDate>
    <dc:creator>CorinneT</dc:creator>
    <dc:date>2022-07-15T20:13:19Z</dc:date>
    <item>
      <title>iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823574#M325191</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Could someone help me, please? I&amp;nbsp;lack method and practice.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'd like to use an iterative loop with the row of data &lt;/SPAN&gt;&lt;SPAN&gt;that &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1)I repeat 12 times, I have the program below:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;* Total number of infusions;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if TT1&amp;nbsp; ne . then TT1_adm = 1 ; else TT1_adm = 0 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; …….&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if TT12&amp;nbsp; ne . then TT12_adm = 1 ; else TT12_adm = 0 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I done, but it not works:&lt;/P&gt;
&lt;P&gt;%let i=1;&lt;/P&gt;
&lt;P&gt;do i=1 to 12;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if TT&amp;amp;i&amp;nbsp; ne . then TT&amp;amp;i _adm = 1 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else TT&amp;amp;i _adm = 0 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nb_tot_adm= sum (of TT&amp;amp;i _adm)&amp;nbsp;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_TTX = sum(of TT&amp;amp;i )&amp;nbsp;;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;2) if I would like, to&amp;nbsp;&lt;SPAN&gt;repeat n times, the same program above, please. How could I go?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thx, in advance for all your answers&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 19:30:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823574#M325191</guid>
      <dc:creator>CorinneT</dc:creator>
      <dc:date>2022-07-15T19:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823575#M325192</link>
      <description>&lt;P&gt;You are trying to make a loop using macros when you should be using arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array tt {12} ;
  array adm {12} tt1_adm tt2_adm tt3_adm tt4_adm  tt5_adm  tt6_adm
                 tt7_adm tt8_adm tt9_adm tt10_adm tt11_adm tt12_adm;

  do i=1 to 12;
   if tt{i} ne . then adm{i}=1;
   else adm{i}=0;
  end;
  nb_tot_adm=sum(of adm{*});
  sum_TTX = sum(of TT{*}) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This first array statement has no specific variables names assigned to the 12 elements of TT, so it assumes TT1, TT2,... TT12.&amp;nbsp; It just appends a 1, then a 2, etc. to the root "tt".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But that technique can't be used when you need 12 variables named TT1_ADM, TT2_ADM, ... TT12_ADM.&amp;nbsp; &amp;nbsp;If you don't insist on those particular names, then I suggest making the second array declaration simple, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array adm {12} TTADM1-TTADM12;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will generate names TTADM1, TTADM2, ... TTADM12.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 19:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823575#M325192</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-15T19:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823576#M325193</link>
      <description>&lt;P&gt;Thank you so much for your fast response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I whant to repeat more than 12 times for exemple n times. I could replace n when I would like by 12 or 20 for example. So, the method with an array doesn't work, when you do know the dimension of array?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have you got another way, please? I I would like generalize to n times&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 20:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823576#M325193</guid>
      <dc:creator>CorinneT</dc:creator>
      <dc:date>2022-07-15T20:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823577#M325194</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/377925"&gt;@CorinneT&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much for your fast response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I whant to repeat more than 12 times for exemple n times. I could replace n when I would like by 12 or 20 for example. So, the method with an array doesn't work, when you do know the dimension of array?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have you got another way, please? I I would like generalize to n times&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't understand what you are saying.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But to answer the question.&amp;nbsp; You KNOW the dimension of an ARRAY before you write the CODE.&amp;nbsp; For the ARRAY statement to work you need to either specify the dimension.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array xx [10];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or list all of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array xx xx1-xx10;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you can explain what you are trying to do then perhaps we can help you write some code.&lt;/P&gt;
&lt;P&gt;So far we appear to have &lt;A href="https://xyproblem.info/" target="_self"&gt;an XY problem&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 20:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823577#M325194</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-15T20:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823580#M325196</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/377925"&gt;@CorinneT&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much for your fast response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I whant to repeat more than 12 times for exemple n times. I could replace n when I would like by 12 or 20 for example. So, the method with an array doesn't work, when you do know the dimension of array?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have you got another way, please? I I would like generalize to n times&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First provide an example input data set an what you expect for the output.&lt;/P&gt;
&lt;P&gt;Your statements in your attempt can be taken to mean several different things are attempted and you need to clarify them. They need clarification because you overwrite the summed values with each iteration and the space between TT&amp;amp;i and the _adm is just plain incorrect syntax and including both of them inside an iteration raises questions about just exactly what sum value you want for the two variables.&lt;/P&gt;
&lt;PRE&gt;nb_tot_adm= sum (of TT&amp;amp;i _adm) ;
 sum_TTX = sum(of TT&amp;amp;i ) ;&lt;/PRE&gt;
&lt;P&gt;If you have any sort of Identification variable, or combination that uniquely identifies the record this will work to build the tt01_adm variables poor of a choice as that is and doesn't require knowing anything about how many TT variables there were. Still need clarification of the totals.&lt;/P&gt;
&lt;PRE&gt;%let n=25;
/* create an example data set*/
data junk;
   array t {*} tt1-tt&amp;amp;n;
   do id= 1 to 10;
      do i=1 to dim(t);
        t[i]=rand('integer',2)-1;
      end;
      output;
   end;
run;
/* given any input data set with some sort of ID
   and those variables
*/
data reshaped;
   set junk;
   by id;
   array t(*) tt: ;
   do i= 1 to dim(t);
      val=t[i];
      vn =vname(t[i]);
      idvar=vn;
      output;
      val=(t[i] ne .);
      idvar=catt(vn,'_adm');
      output;
   end;
   drop  tt: ;
run;

proc transpose data=reshaped out=trans(drop=_name_);
   by id;
   var val;
   id idvar;
run;

&lt;/PRE&gt;
&lt;P&gt;better for building the adm variables would be&lt;/P&gt;
&lt;PRE&gt;data reshaped;
   set junk;
   by id;
   array t(*) tt: ;
   do i= 1 to dim(t);
      val=t[i];
      vn =vname(t[i]);
      idvar=vn;
      output;
      val=(t[i] ne .);
      idvar=catt('adm_',vn);
      /* or 
      idvar=catt('tt_adm',i);
      */
      output;
   end;
   drop  tt: ;
run;&lt;/PRE&gt;
&lt;P&gt;It may be that another pass through the data using&lt;/P&gt;
&lt;P&gt;sum(of tt: )&lt;/P&gt;
&lt;P&gt;sum(of adm_tt: )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are intending to do some sort of diaganol sum of tt1, tt1-tt2, tt1-tt3, tt1-tt4 , ..., tt1-ttN then that needs a better description (and can be done in the Reshaped data step)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 20:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823580#M325196</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-15T20:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823584#M325199</link>
      <description>&lt;P&gt;Technically, you can do this using macros similar to the way you began.&amp;nbsp; However, it is not recommended for inexperienced programmers.&amp;nbsp; You are likely to make many mistakes and syntax errors along the way, enough to discourage you from wanting to use this approach.&amp;nbsp; For the record, though, here is what it might look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop (n=);
   %local i;
   nb_tot_adm = 0;
   sum_TTX = 0;
   %do i=1 to &amp;amp;n;
      iif TT&amp;amp;i ne . then TT&amp;amp;i._adm = 1;
      else TT&amp;amp;i._adm=0;
      nb_tot_adm + TT&amp;amp;i._adm;
      sum_TTX + TT&amp;amp;i;
   %end;
data want;
set have;
%loop (n=12)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can change the value for N when you call the macro, choosing whatever is appropriate for your current data set.&amp;nbsp; Also, you don't necessarily have to create 12 sets of variables.&amp;nbsp; One set would do, since you can accumulate their values inside the loop. instead of waiting to create them all first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, from&amp;nbsp; your origianl post it's not 100% clear that you have 12 variables to begin with.&amp;nbsp; Perhaps you actually have 1 variable with 12 observations.&amp;nbsp; The programming would be very different in that case, and very easy.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 21:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823584#M325199</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-07-15T21:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: iterative Do loops.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823585#M325200</link>
      <description>&lt;P&gt;Thanks. It's what I'm pending. I will run it tomorrow.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 21:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-Do-loops/m-p/823585#M325200</guid>
      <dc:creator>CorinneT</dc:creator>
      <dc:date>2022-07-15T21:43:47Z</dc:date>
    </item>
  </channel>
</rss>

