Hi,
I would like to make program in order to do the following in the financial dataset using a sort of loop.
A
1992 firm A B0=0.2
1993 firm A B1=0.5*B0 + A,1993 25
1994 firm A B2=0.5*B1 + A, 1994 30
1995 firm A B3=0.5*B2 + A, 1995 40
2002 firm B B0=0.3
2003 firm B B1=0.5*B0 + A,2003 50
2004 firm B B2=0.5*B1 + A,2004 100
2005 firm B B3=0.5*B2 + A,2005 150
It will be very appreciative if you can help me.
Like this?
data have;
input year firm $ factor A;
datalines;
1992 A 0.2 .
1993 A 0.5 25
1994 A 0.5 30
1995 A 0.5 40
2002 B 0.3 .
2003 B 0.5 50
2004 B 0.5 100
2005 B 0.5 150
;
data want;
set have;
by firm;
if first.firm then B = factor;
else B = factor*prevB + A;
retain prevB;
prevB = B;
drop prevB;
run;
proc print data=want noobs; run;
year firm factor A B 1992 A 0.2 . 0.200 1993 A 0.5 25 25.100 1994 A 0.5 30 42.550 1995 A 0.5 40 61.275 2002 B 0.3 . 0.300 2003 B 0.5 50 50.150 2004 B 0.5 100 125.075 2005 B 0.5 150 212.538
With sample data this becomes much clearer.
Though please test your data step before posting, you list 5 variables but only have four.
This worked for me - I dropped the 'factor' whatever that was supposed to be.
data want;
set have;
retain B1;
if not missing(B0) then B1=B0;
else B1 = B1*0.5 + A;
drop B0;
run;
@joon1 wrote:
Hi PGStats,Thank you so much for your helpMy actual data is as follows. 0.5 is just constant and it can be included in the program. I am not sure where you defined 'prevB' in the previous program.data have;
input year firm $ factor B0 A; *<- 5 variables listed?;
datalines;
1992 A 0.2 .
1993 A 25
1994 A 30
1995 A 40
2002 B 0.3 .
2003 B 50
2004 B 100
2005 B 150
I need to start from B0 and compute Bt in each year.1992 B0=0.21993 B1=0.5*B0 + A(25)1994 B2=0.5*B1 + A(30)1995 B3=0.5*B2 + A(40)2002 B0=0.32003 B1=0.5*B0 + A(50)2004 B2=0.5*B1 + A(100)2005 B3=0.5*B2 + A(150)It will be grateful if you can help me a bit more.Sincerely,Joon1
Thank you so much again
However, be0 should remain in the first year when observation of a company is available. If the code you provided is used, B0 is dropped and B1(be1) is computed for the first year. B1 should be computed from 1984 for the company with gvkey 1001 and 1983 for the company with gvkey 1003. Could you please help me this part? Thank you again
Obs GVKEY FYEAR XAD be11234567891011
001001 | 1983 | 1.3300 | -3.325 |
001001 | 1984 | 1.8400 | 0.178 |
001001 | 1985 | 3.0390 | 3.128 |
001003 | 1982 | 0.1610 | -0.403 |
001003 | 1983 | 0.1540 | -0.047 |
001003 | 1984 | 0.2150 | 0.191 |
001003 | 1985 | 1.0260 | 1.122 |
001003 | 1986 | 1.7720 | 2.333 |
001003 | 1987 | 2.3980 | 3.564 |
001003 | 1988 | 2.1080 | 3.890 |
001003 | 1989 | 1.0070 | 2.952 |
001001 | 1983 | 1.3300 | -3.3250 |
001001 | 1984 | 1.8400 | . |
001001 | 1985 | 3.0390 | . |
001003 | 1982 | 0.1610 | -0.4025 |
001003 | 1983 | 0.1540 | . |
001003 | 1984 | 0.2150 | . |
001003 | 1985 | 1.0260 | . |
001003 | 1986 | 1.7720 | . |
001003 | 1987 | 2.3980 | . |
001003 | 1988 | 2.1080 | . |
001003 | 1989 | 1.0070 |
167 data s7;
168 set s6;
169 by gvkey;
170 retain be1;
171 if not missing(be0) then be1=be0;
172 else be1 = be1*0.5 + xad;
173 drop=be0;
174 run;
NOTE: There were 78993 observations read from the data set WORK.S6.
NOTE: The data set WORK.S7 has 78993 observations and 25 variables.
NOTE: DATA statement used (Total process time):
real time 0.15 seconds
cpu time 0.04 seconds
The following is output.
Obs GVKEY FYEAR XAD be11234567891011001001 | 1983 | 1.3300 | -3.325 |
001001 | 1984 | 1.8400 | 0.178 |
001001 | 1985 | 3.0390 | 3.128 |
001003 | 1982 | 0.1610 | -0.403 |
001003 | 1983 | 0.1540 | -0.047 |
001003 | 1984 | 0.2150 | 0.191 |
001003 | 1985 | 1.0260 | 1.122 |
001003 | 1986 | 1.7720 | 2.333 |
001003 | 1987 | 2.3980 | 3.564 |
001003 | 1988 | 2.1080 | 3.890 |
001003 | 1989 | 1.0070 | 2.952 |
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.