BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joon1
Quartz | Level 8

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
The numbers are correct then,just not in the right variables? If so You should be able to fix it yourself. There’s enough info there to try adjusting the IF condition. Also you should understand your code.

View solution in original post

11 REPLIES 11
Reeza
Super User
We can't really help given what you've shown. What are you starting with? What do you want as output? What approach have you tried already? Please show any code.
PGStats
Opal | Level 21

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
PG
joon1
Quartz | Level 8
Hi PGStats,
 
Thank you so much for your help
 
My 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;
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.2
1993 B1=0.5*B0 + A(25)
1994  B2=0.5*B1 + A(30)
1995  B3=0.5*B2 + A(40)
2002  B0=0.3
2003  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
Reeza
Super User

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 help
 
My 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.2
1993 B1=0.5*B0 + A(25)
1994  B2=0.5*B1 + A(30)
1995  B3=0.5*B2 + A(40)
2002  B0=0.3
2003  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

 

joon1
Quartz | Level 8
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
Reeza
Super User
Post the exact results you want for the sample data you posted. My answer matches your initial post, as far as I can see. I see you’ve posted new data but again no indication of what’s input vs output or how this would align with your previous data.
joon1
Quartz | Level 8

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

00100119831.3300-3.325
00100119841.84000.178
00100119853.03903.128
00100319820.1610-0.403
00100319830.1540-0.047
00100319840.21500.191
00100319851.02601.122
00100319861.77202.333
00100319872.39803.564
00100319882.10803.890
00100319891.00702.952
joon1
Quartz | Level 8
Thank you so much for your prompt response

 

 
My actual data is as follows.
 
data have;
input year firm $ B0 A;
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.
                                               B
1992  B0=0.2                          0.2
1993 B1=0.5*B0 + A(25)        0.2*0.5+25=25.1
1994  B2=0.5*B1 + A(30)        25.1*0.5+30=42.55
1995  B3=0.5*B2 + A(40)        42.55*0.5+40=83.05
2002  B0=0.3                          0.3
2003  B1=0.5*B0 + A(50)        0.3*0.5 +50=50.15
2004  B2=0.5*B1 + A(100)       50.15*0.5+100=125.075
2005 B3=0.5*B2 + A(150)        125.075*0.5+150=212.5375

 

Reeza
Super User
Those are the exact numbers in my output, is that not what you get? If not post the code and log.
joon1
Quartz | Level 8
Hi Reeza,
 
The following is the dataset.
Obs GVKEY FYEAR XAD be01234567891011
00100119831.3300-3.3250
00100119841.8400.
00100119853.0390.
00100319820.1610-0.4025
00100319830.1540.
00100319840.2150.
00100319851.0260.
00100319861.7720.
00100319872.3980.
00100319882.1080.
00100319891.0070
 
The following is the program and log.

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 be11234567891011
00100119831.3300-3.325
00100119841.84000.178
00100119853.03903.128
00100319820.1610-0.403
00100319830.1540-0.047
00100319840.21500.191
00100319851.02601.122
00100319861.77202.333
00100319872.39803.564
00100319882.10803.890
00100319891.00702.952
 
be0 should remain in 1983 for gvkey 1001 firm and in 1982 for gvkey 1003. be1 should be computed from 1984 for gvkey 1001 firm and 1983 for gvkey 1984 firm.
 
It will be appreciative if you can help me to fix this issue.
 
Sincerely,
Joon1
Reeza
Super User
The numbers are correct then,just not in the right variables? If so You should be able to fix it yourself. There’s enough info there to try adjusting the IF condition. Also you should understand your code.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 1605 views
  • 0 likes
  • 3 in conversation