<?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 with conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351343#M81780</link>
    <description>&lt;P&gt;You already have a good solution, but I wrote this, thus might as well share it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format;
  invalue price
  0.90-high=.
  ;
run;

data have (keep=period);
  infile cards dlm='09'x;
  informat Price_1-Price_11 price4.;
  input Price_1-Price_11;
  period=coalesce(of price_:);
  cards;
0.95	0.96	0.94	0.93	0.92	0.91	0.88	0.91	0.89	0.88	0.93
;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Apr 2017 15:35:06 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-04-19T15:35:06Z</dc:date>
    <item>
      <title>Do loop with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351333#M81776</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;I've got a dataset with 100s of price fields. I want to loop through all price fields and identify the first where the price is less than 0.9 and save the number after "price_" as period. In the row below, the period would be 0.88.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 528pt;" border="0" width="704" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="11" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl63" style="height: 15.0pt; width: 48pt;"&gt;Price_1&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_2&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_3&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_4&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_5&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_6&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_7&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_8&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_9&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_10&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;Price_11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;0.95&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.96&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.94&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.93&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.92&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.91&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.88&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.91&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.89&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.88&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;0.93&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:13:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351333#M81776</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-04-19T15:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351336#M81777</link>
      <description>&lt;P&gt;It sounds like you just want the index into the array. So given this dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input price_1 - price_11 ;
cards;
0.95 0.96 0.94 0.93 0.92 0.91 0.88 0.91 0.89 0.88 0.93
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could create PERIOD using code like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array p price_1 - price_11;
  do i=1 to dim(p) while (period=.);
    if p(i) &amp;lt; .90 then period =i;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could use another variable to store the actual value if you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array p price_1 - price_11;
  do i=1 to dim(p) while (period=.);
    if p(i) &amp;lt; .90 then do;
      period =i;
      first_low_price = p(i);
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351336#M81777</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-19T15:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351340#M81778</link>
      <description>&lt;PRE&gt;/* creating data */
data a; price_1=0.95; price_2=0.96; price_3=0.94; price_4=0.93; price_5=0.92; price_6=0.91; price_7=0.88; 
  price_8=0.91 ; price_9=0.89; price_10=0.88; price_11=0.93;  run;
/* finding price_: variable with first value less than .9
     saving value
     saving variable name
   and stopping 
*/
data a;
set a;
length vNameOf_firstPriceLTPoint9  $32 ;
array prices {*} price_:;
n=dim(prices);
do i = 1 to n;
 if prices{i}&amp;lt;0.9 then do ;
  first_price_less_than_point9=prices{i};
  vNameOf_firstPriceLTPoint9=vname(prices{i});
  leave;
 end;
end; 
run;&lt;/PRE&gt;
&lt;P&gt;This code stops looping after finding, and&amp;nbsp;gives you the name of the variable who's value is less than 0.9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351340#M81778</guid>
      <dc:creator>JerryV</dc:creator>
      <dc:date>2017-04-19T15:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351341#M81779</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;I've got a dataset with 100s of price fields. I want to loop through all price fields and identify the first where the price is less than 0.9 and save the number after "price_" as period. In the row below, the period would be 0.88.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="704" style="width: 528pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL width="64" style="width: 48pt;" span="11" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD width="64" height="20" class="xl63" style="width: 48pt; height: 15pt;"&gt;Price_1&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_2&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_3&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_4&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_5&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_6&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_7&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_8&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_9&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_10&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt; border-left-color: currentColor; border-left-width: medium; border-left-style: none;"&gt;Price_11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15pt; border-top-color: currentColor; border-top-width: medium; border-top-style: none;"&gt;0.95&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.96&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.94&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.93&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.92&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.91&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.88&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.91&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.89&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.88&lt;/TD&gt;
&lt;TD align="right" class="xl63" style="border-top-color: currentColor; border-left-color: currentColor; border-top-width: medium; border-left-width: medium; border-top-style: none; border-left-style: none;"&gt;0.93&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;From your text wouldn't period be&amp;nbsp; 7? The number after price_ where the value is 0.88 for variable Price_7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A minor change to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s code:&lt;/P&gt;
&lt;PRE&gt;data want ;
   set have ;
   array p price_1 - price_11;
   do i=1 to dim(p);
      if p(i) &amp;lt; .90 then do;
         period =i;
         leave;
      end;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;which may work better if you already have a variable named Period and need to refresh the value. The LEAVE instruction says to interrupt the do loop at the current execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351341#M81779</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-19T15:29:45Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351343#M81780</link>
      <description>&lt;P&gt;You already have a good solution, but I wrote this, thus might as well share it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format;
  invalue price
  0.90-high=.
  ;
run;

data have (keep=period);
  infile cards dlm='09'x;
  informat Price_1-Price_11 price4.;
  input Price_1-Price_11;
  period=coalesce(of price_:);
  cards;
0.95	0.96	0.94	0.93	0.92	0.91	0.88	0.91	0.89	0.88	0.93
;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351343#M81780</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-19T15:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351359#M81783</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;super slick, a conventional thinker could have never though. Class act. I wish I had your brains&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-with-conditions/m-p/351359#M81783</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-19T16:07:40Z</dc:date>
    </item>
  </channel>
</rss>

