## how to retain the previous value in formula ?

Occasional Contributor
Posts: 7

# how to retain the previous value in formula ?

Hi _ALL_

THIS IS VIKRAM, I'M  HAVING GENERAL DOUBT  HOW TO USE  PREVIOUS VALUE ALONG WITH PRESENT VALUE IN FORMULAS, FROM BELOW FORMULA

IN FORMULA INITIAL INC1=0 AZC1=0 and INC=" INC VALUE OF THE DATE "  AZC=" AZC VALUE OF THE DATE"

data XYZ (drop=INC1 AZC1);

set sasuser.Dups;

if DATE<first.DATE then INC1=INC and AZC1=AZC;

else IF DATE=first.DATE then INC=INC and AZC=AZC

retain INC1 0;

retain AZC 0;

X=SVYM*(sin((INC1+INC)/2)* cos((AZC1+AZC)/2));

Y=SVYM*(sin((INC1+INC)/2)*sin((AZC1+AZC)/2));

Z=SVYM*(cos((INC1+INC)/2));

run;

 DATE SVYM INC AZC 7-Oct-13 5886.1 54.74 354.39 9-Oct-13 0 0 6.281614 13-Oct-13 115.8758 0.009425 4.739615 14-Oct-13 109.1062 0.01117 4.509232 15-Oct-13 331.9821 0.001047 4.536634 16-Oct-13 397.764 0.001571 0.387114 26-Oct-13 419.0695 0.025831 0.092328 27-Oct-13 562.2066 0.220435 0.09512 28-Oct-13 648.081 0.33999 0.007505 29-Oct-13 677.1528 0.381354 6.281439 30-Oct-13 791.9771 0.524122 6.255957 31-Oct-13 849.377 0.518014 6.190856 1-Nov-13 820.9818 0.532325 6.224367 2-Nov-13 891.6223 0.531976 6.174974 3-Nov-13 978.9201 0.502655 6.201503 4-Nov-13 3307.3 29.4 356.24 5-Nov-13 2474.42 28.83 359.32 13-Nov-13 3987.86 27.43 2.18 14-Nov-13 4281.31 22.69 354.24 15-Nov-13 4467.21 19.39 347.46 16-Nov-13 4754.58 13.9 340.39 17-Nov-13 4849.92 12.23 338.14 18-Nov-13 4921 10.5 338.14 27-Nov-13 4941.77 10.33 338.65 28-Nov-13 5310.93 1.04 155.3 29-Nov-13 5615.35 3.77 142.08 30-Nov-13 5711.57 3.59 145.03 14-Dec-13 4754.58 13.9 340.39 18-Dec-13 5069.8 9.3 301.11 19-Dec-13 5356.22 7.6 305.78 20-Dec-13 5549.31 5.51 307.16
Super User
Posts: 13,524

## Re: how to retain the previous value in formula ?

You have some issues:

First.date will have values of 0 or 1 depending on whether the current date is the first one or not. So it is unlikely that you want to make the comparison

date < first.date

as it will only be true for dates before 1960.

Also to use the first.date notation you generally need to use BY DATE, and the data should be sorted by date.

If you are trying to set values for INC1 and AZC1 from the first record and keep them then:

set sasuser.dups;

Retain INC1 0;

Retain AZC1 0;

If _n_ = 1 then do;

INC1 = INC;

AZC1 = AZC;

end;

and

else IF DATE=first.DATE then INC=INC and AZC=AZC   is not needed. It also would likely generate an error as the semicolon was missing.

Discussion stats