how to retain the previous value in formula ?

Reply
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;

DATESVYMINCAZC
7-Oct-135886.154.74354.39
9-Oct-13006.281614
13-Oct-13115.87580.0094254.739615
14-Oct-13109.10620.011174.509232
15-Oct-13331.98210.0010474.536634
16-Oct-13397.7640.0015710.387114
26-Oct-13419.06950.0258310.092328
27-Oct-13562.20660.2204350.09512
28-Oct-13648.0810.339990.007505
29-Oct-13677.15280.3813546.281439
30-Oct-13791.97710.5241226.255957
31-Oct-13849.3770.5180146.190856
1-Nov-13820.98180.5323256.224367
2-Nov-13891.62230.5319766.174974
3-Nov-13978.92010.5026556.201503
4-Nov-133307.329.4356.24
5-Nov-132474.4228.83359.32
13-Nov-133987.8627.432.18
14-Nov-134281.3122.69354.24
15-Nov-134467.2119.39347.46
16-Nov-134754.5813.9340.39
17-Nov-134849.9212.23338.14
18-Nov-13492110.5338.14
27-Nov-134941.7710.33338.65
28-Nov-135310.931.04155.3
29-Nov-135615.353.77142.08
30-Nov-135711.573.59145.03
14-Dec-134754.5813.9340.39
18-Dec-135069.89.3301.11
19-Dec-135356.227.6305.78
20-Dec-135549.315.51307.16
Super User
Posts: 10,540

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.

Ask a Question
Discussion stats
  • 1 reply
  • 182 views
  • 1 like
  • 2 in conversation