BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jcdataguy
Fluorite | Level 6

Hi experts!  I need help with this while loop. Can u tell visually what is wrong with it? It should iterate through two arrays (flow_field and bal_field) and loop while "switch_in" is more than zero:

 

data do_until;
set money_table;

array flow_field [*]  _from_1  _from_2  _from_3   _from_4  _from_5 ;
array bal_field [*]   bal_1 bal_2 bal_3  bal_4  bal_5 ;

switch_in = 100000;

do while (switch_in > 0);
  do i=1 to 5;
     if bal_field[i] lt 0 and bal_field[i] ne . then do;
         flow_field[i] = min(switch_in, abs(bal_field[i]));
        switch_in = switch_in - (abs(bal_field[i]));
      end;
  end;
end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Looks like you are expecting switch_in to increment between rows. You need to add a RETAIN statement to make this happen (position is not important):

retain switch_in;

 

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

Looks like you are expecting switch_in to increment between rows. You need to add a RETAIN statement to make this happen (position is not important):

retain switch_in;

 

jcdataguy
Fluorite | Level 6

Thanks for your reply @SASKiwi .  But I want Switch_In to decrease with each passing of teh loop until it reaches 0. It loop it's supposed to iterate for those bal_flow that are less than 0. 

jcdataguy
Fluorite | Level 6

HERE IS SOME SAMPLE DATA:

The total values in the _from fields cannot exceed the switch_in values

SASKiwi
PROC Star

@jcdataguy - An increment can be either positive or negative but you will still need the RETAIN for it to work.

Astounding
PROC Star

One possibility (at least in theory):  Once you hit an observation that has all non-negative balance fields, the logic fails to change switch_in.  The program becomes an infinite loop.

Patrick
Opal | Level 21

If for a specific row below condition is never true....

if bal_field[i] lt 0 and bal_field[i] ne . then

...then the value for switch_in never changes but remains 100000 which results in an infinite loop.

 

Here some code to test this using the sample data you've posted.

DATA money_table;
  LENGTH
    BAL_1              8
    BAL_2              8
    BAL_3              8
    BAL_4              8
    BAL_5              8
    total_netbal       8
    _FROM_1            8
    FROM_2             8
    _FROM_3            8
    FROM_4             8
    _FROM_5            8
    SWITCH_IN          8;
  FORMAT
    BAL_1            DOLLAR19.1
    BAL_2            DOLLAR19.1
    BAL_3            DOLLAR19.1
    BAL_4            DOLLAR19.1
    BAL_5            DOLLAR16.2
    total_netbal     DOLLAR19.1
    _FROM_1          DOLLAR19.1
    FROM_2           DOLLAR19.1
    _FROM_3          DOLLAR19.1
    FROM_4           DOLLAR19.1
    _FROM_5          DOLLAR19.1
    SWITCH_IN        BEST12.;
  INFORMAT
    BAL_1            DOLLAR19.
    BAL_2            DOLLAR19.
    BAL_3            DOLLAR19.
    BAL_4            DOLLAR19.
    BAL_5            DOLLAR16.
    total_netbal     DOLLAR19.
    _FROM_1          DOLLAR19.
    FROM_2           DOLLAR19.
    _FROM_3          DOLLAR19.
    FROM_4           DOLLAR19.
    _FROM_5          DOLLAR19.
    SWITCH_IN        BEST12.;
  INFILE DATALINES4
    DLM='7F'x
    MISSOVER
    DSD;
  INPUT
    BAL_1            : BEST32.
    BAL_2            : BEST32.
    BAL_3            : BEST32.
    BAL_4            : BEST32.
    BAL_5            : DOLLAR19.
    total_netbal     : BEST32.
    _FROM_1          : BEST32.
    FROM_2           : BEST32.
    _FROM_3          : BEST32.
    FROM_4           : BEST32.
    _FROM_5          : BEST32.
    SWITCH_IN        : BEST32.;
  DATALINES4;
542.5958829.64288.119999999999. 59660.3558829.640...5000
899.470.. 899.4700...10000
1285.2579059.13-4543.95. 75800.4375800.433258.70000000001003258.7000000000115000
-61360.7152471.48.. 91110.7891110.7861360.761360.70020000
8021.2157220.79.34.1599999999999 65276.1657220.790...25000
-15165.2540021.09.. 24855.8424855.8415165.2515165.250030000
141.0438611.91-882.77. 37870.1837870.18741.72999999999600741.72999999999635000
-9890.78124218.75.. 114327.97114327.979890.789890.780040000
-15385.8880865.71.. 65479.8365479.8315385.8815385.880045000
;;;;

data do_until;
  obs=_n_;
  set money_table;
  array flow_field [*]  _from_1  _from_2  _from_3   _from_4  _from_5;
  array bal_field [*]   bal_1 bal_2 bal_3  bal_4  bal_5;
  switch_in = 100000;

  do while (switch_in > 0);
    do i=1 to dim(flow_field);
      if bal_field[i] lt 0 and bal_field[i] ne . then
        do;
          flow_field[i] = min(switch_in, abs(bal_field[i]));
          switch_in = switch_in - (abs(bal_field[i]));
/*          output;*/
        end;
        output;
    end;
    n=sum(n,1);
    if n>1000 then leave;
    keep i obs switch_in;
  end;
run;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1774 views
  • 2 likes
  • 4 in conversation