tags:

views:

41

answers:

2

The problem, that if the else is executing, increment of S will not accomplish. Any idea?

data osszes_folyositas;
 set osszes_tabla;
 retain old_xname;
 retain s 0;

 if xname ne old_xname  then
 do;
  old_xname = xname; 
  s = 0;
 end;
 else
 do; 
  s = s + Foly_s_tott_t_rgyh_ban_HUF;
  delete;
 end;
run;
+1  A: 

Not sure what you are trying to do. But if you have your records ordered by "xname", and for each group of "xname" just want to sum across a value, you could try the following.

data sample;

    input xname$1-6 myvalue;

datalines;    
name01 5
name01 1
name02 3
name02 8
name02 4
name03 7
;

data result;
    set sample;
    by xname;

    retain s 0;

    if first.xname then s=0;
    s=s+myvalue;
    if last.xname then output;
run;

proc print data=result;
run;

This resets "s" for each group of "xname" and outputs the last record with "s" set to the sum of "myvalue" across the group. The result looks like this:

Obs    xname     myvalue     s

 1     name01       1        6
 2     name02       4       15
 3     name03       7        7
Michael Hasan
Thanks, it works!
Kukoda János
A: 

This kind of tasks can be best handled with a programming pattern known as DoW (aka Do loop of Whitlock). For each by-group, the initialization comes before the loop and the observation output after the loop. Can you see how it really works out? This paper is old but a must-read.

data sample;
   input xname$1-6 myvalue;
datalines;    
name01 5
name01 1
name02 3
name02 8
name02 4
name03 7
;
run;

proc sort data=sample;
   by xname;
run;

data result;
   if 0 then set sample; /* prep pdv */
   s = 0;   
   do until (last.xname);
      set sample;
      by xname;
      s + myValue;
   end;
run;

/* check */
proc print data=result;
run;
/* on lst
Obs    xname     myvalue     s

  1     name01       1        6
  2     name02       4       15
  3     name03       7        7
*/
Chang Chung