Solved
Contributor
Posts: 65

# sum calculation from an equation

[ Edited ]

Hello;

I have the incomplete gamma function (IGF) which is:

Y = a * (t ** b) * exp (-c * t)

and a data like this:

id     a      b        c

what I am looking for is to calculate P150  which is the sum of Yt  from t=1 to t=150

using params a b and c and get this:

id      a      b       c       P150

is there a method, with SAS, which allows me to do this without using Excel (which I often do).

I always found help in this forum. Thank you in advance.

Accepted Solutions
Solution
‎06-10-2016 06:10 PM
Posts: 5,541

## Re: sum calculation from an equation

Time to upgrade! Once a function is defined with FCMP, it can be used in great many places, including, for example, in SQL

``````data myData;
input id :\$12.	a	b	c;
datalines;
T0600021251	16.79361	0.04351	0.00253
T2100050954	10.62616	0.24511	0.00231
T2100071545	12.54940	0.21685	0.00301
T2300051246	15.35562	0.15303	0.00306
T2300051372	21.58488	0.05164	0.00168
T2300051449	24.43391	0.00001	0.00269
T2300051462	14.47446	0.18741	0.00365
T2300051496	32.75656	0.00001	0.00236
T2300051533	19.42636	0.00001	0.00212
T2300051540	20.75340	0.04351	0.00229
T2300051622	8.27472	0.25447	0.00237
T2300051655	7.73511	0.31439	0.00418
T2300051846	8.87238	0.26569	0.00306
T2300051849	26.85240	0.00001	0.00127
T2300056142	10.45094	0.32418	0.00475
T2300057352	22.15670	0.03133	0.00310
T2300058009	15.46157	0.12580	0.00212
;

proc fcmp outlib=sasuser.fcmp.math;
function P150(a, b, c);
do t = 1 to 150;
r + a * (t ** b) * exp (-c * t);
end;
return (r);
endsub;
run;

options cmplib=sasuser.fcmp;

proc sql;
select *, P150(a, b, c) as d
from myData;
quit;
``````
```                id                   a         b         c         d
T0600021251   16.79361   0.04351   0.00253  2487.253
T2100050954   10.62616   0.24511   0.00231   3633.33
T2100071545    12.5494   0.21685   0.00301  3614.663
T2300051246   15.35562   0.15303   0.00306  3396.119
T2300051372   21.58488   0.05164   0.00168  3514.853
T2300051449   24.43391   0.00001   0.00269  3011.899
T2300051462   14.47446   0.18741   0.00365  3520.605
T2300051496   32.75656   0.00001   0.00236  4133.227
T2300051533   19.42636   0.00001   0.00212  2493.532
T2300051540    20.7534   0.04351   0.00229  3127.748
T2300051622    8.27472   0.25447   0.00237  2927.307
T2300051655    7.73511   0.31439   0.00418  3039.666
T2300051846    8.87238   0.26569   0.00306  3113.042
T2300051849    26.8524   0.00001   0.00127  3665.269
T2300056142   10.45094   0.32418   0.00475  4090.179
T2300057352    22.1567   0.03133    0.0031  3001.778
T2300058009   15.46157    0.1258   0.00212  3287.251
```
PG

All Replies
Posts: 5,541

## Re: sum calculation from an equation

[ Edited ]

Assuming the sum is over integer values of t :

``````proc fcmp outlib=sasuser.fcmp.math;
function P150(a, b, c);
do t = 1 to 150;
r + a * (t ** b) * exp (-c * t);
end;
return (r);
endsub;
run;

options cmplib=sasuser.fcmp;

data _null_;
a = 1; b = 2; c = 3;
y = P150(a, b, c);
put _all_;
run;

``````

PG
Contributor
Posts: 65

## Re: sum calculation from an equation

Thank you for your reponce PG,

Sorry, I think that the SAS version that I have (9.0) does not include the procedure FCMP
and Second, you set the values of a, b and c where as in my data file these values  change from one line to another.

this is how my file looks:

 id a b c T0600021251 16.79361 0.04351 0.00253 T2100050954 10.62616 0.24511 0.00231 T2100071545 12.54940 0.21685 0.00301 T2300051246 15.35562 0.15303 0.00306 T2300051372 21.58488 0.05164 0.00168 T2300051449 24.43391 0.00001 0.00269 T2300051462 14.47446 0.18741 0.00365 T2300051496 32.75656 0.00001 0.00236 T2300051533 19.42636 0.00001 0.00212 T2300051540 20.75340 0.04351 0.00229 T2300051622 8.27472 0.25447 0.00237 T2300051655 7.73511 0.31439 0.00418 T2300051846 8.87238 0.26569 0.00306 T2300051849 26.85240 0.00001 0.00127 T2300056142 10.45094 0.32418 0.00475 T2300057352 22.15670 0.03133 0.00310 T2300058009 15.46157 0.12580 0.00212 …

30588 different id.

Solution
‎06-10-2016 06:10 PM
Posts: 5,541

## Re: sum calculation from an equation

Time to upgrade! Once a function is defined with FCMP, it can be used in great many places, including, for example, in SQL

``````data myData;
input id :\$12.	a	b	c;
datalines;
T0600021251	16.79361	0.04351	0.00253
T2100050954	10.62616	0.24511	0.00231
T2100071545	12.54940	0.21685	0.00301
T2300051246	15.35562	0.15303	0.00306
T2300051372	21.58488	0.05164	0.00168
T2300051449	24.43391	0.00001	0.00269
T2300051462	14.47446	0.18741	0.00365
T2300051496	32.75656	0.00001	0.00236
T2300051533	19.42636	0.00001	0.00212
T2300051540	20.75340	0.04351	0.00229
T2300051622	8.27472	0.25447	0.00237
T2300051655	7.73511	0.31439	0.00418
T2300051846	8.87238	0.26569	0.00306
T2300051849	26.85240	0.00001	0.00127
T2300056142	10.45094	0.32418	0.00475
T2300057352	22.15670	0.03133	0.00310
T2300058009	15.46157	0.12580	0.00212
;

proc fcmp outlib=sasuser.fcmp.math;
function P150(a, b, c);
do t = 1 to 150;
r + a * (t ** b) * exp (-c * t);
end;
return (r);
endsub;
run;

options cmplib=sasuser.fcmp;

proc sql;
select *, P150(a, b, c) as d
from myData;
quit;
``````
```                id                   a         b         c         d
T0600021251   16.79361   0.04351   0.00253  2487.253
T2100050954   10.62616   0.24511   0.00231   3633.33
T2100071545    12.5494   0.21685   0.00301  3614.663
T2300051246   15.35562   0.15303   0.00306  3396.119
T2300051372   21.58488   0.05164   0.00168  3514.853
T2300051449   24.43391   0.00001   0.00269  3011.899
T2300051462   14.47446   0.18741   0.00365  3520.605
T2300051496   32.75656   0.00001   0.00236  4133.227
T2300051533   19.42636   0.00001   0.00212  2493.532
T2300051540    20.7534   0.04351   0.00229  3127.748
T2300051622    8.27472   0.25447   0.00237  2927.307
T2300051655    7.73511   0.31439   0.00418  3039.666
T2300051846    8.87238   0.26569   0.00306  3113.042
T2300051849    26.8524   0.00001   0.00127  3665.269
T2300056142   10.45094   0.32418   0.00475  4090.179
T2300057352    22.1567   0.03133    0.0031  3001.778
T2300058009   15.46157    0.1258   0.00212  3287.251
```
PG
Contributor
Posts: 65

## Re: sum calculation from an equation

Yes PG Stats, sure, I should do it , when strong people like you offered me some great solutions to my problems. thank you so much. Just an explanation, for people who are interested to my question; This solution is interesting for the calculation of the cumulative amount that changes with time. in my case I use it to calculate the amount of milk production at different date using the wood or Wilmink function.

thank you again PG stats, and good luck readers.

Posts: 1,256

## Re: sum calculation from an equation

@soumri: Thanks for pointing out the application area.

If you don't have PROC FCMP at your disposal (yet), you can simply copy the DO loop from PG's PROC FCMP step into his data step after the INPUT statement and an additional assignment statement r=0; (to initialize variable r, which is automatically retained due to the sum statement).

Contributor
Posts: 65

## Re: sum calculation from an equation

[ Edited ]
Posted in reply to FreelanceReinhard

Contributor
Posts: 65

## Re: sum calculation from an equation

``````data want;set have;
r=0;
do t = 1 to 150;
r + al * (t ** bl) * exp (-cl * t);
end;
run;``````

it's ok

Contributor
Posts: 65

## Re: sum calculation from an equation

Hello PGStats;

do you explain me please the utility of using a proc fcmp, while the concept proposed by @FreelanceReinhard renders results numerically equal.
What is the difference.

Thank you for your help.

Posts: 1,256

## Re: sum calculation from an equation

I think, PG has already mentioned the key benefit of the FCMP approach when he wrote: "Once a function is defined with FCMP, it can be used in great many places, including, for example, in SQL".

Indeed, you can call the function, across programs, in a data step without copying or remembering the formula and without the risk of name conflicts between variables used in the function definition and other data step variables. Moreover, you can call it in places where it would be impossible to paste the defining SAS code, e.g. in WHERE conditions (in both DATA and PROC steps as well as dataset options), nested with other functions, in a macro context (via %SYSFUNC), a format definition (with SAS 9.3 or higher) or in PROC SQL, as has been demonstrated.

The numerical results are equal to those from the data step DO loop.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 9 replies
• 506 views
• 5 likes
• 3 in conversation