BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abdulla
Pyrite | Level 9
gvkeyyearRET
10041992-0.04762
100419930.0138
10041994-0.00429
10041995-0.14493
100419960.067797
100419970.107302
100419980.072464
100419990.057027 
133119920.038333
13311993-0.05405
13311994-0.04286
13311995-0.03343
13311996-0.03125
244561992-0.02621
2445619930.006377
2445619940
2445619950.05
2445619960.060317

 

I have the above data and aI want the following result

 

gvkeyyearRETret_cut_dummy
10041992-0.047620
100419930.01380
10041994-0.004291
10041995-0.144931
100419960.0677970
100419970.1073020
100419980.0724641
100419990.0570271
133119920.0383330
13311993-0.054051
13311994-0.042860
13311995-0.033430
13311996-0.031250
244561992-0.026210
2445619930.0063770
24456199401
2445619950.050
2445619960.0603170

 

So, I want  dummy variable equals one if there is a reduction in annual ret, and zero otherwise. I use the following code but I am not getting 0 for the first gvkey. Though my data are sorted by gvkey and year, the result I am getting is not by year. 

Data want;

set have;

by gvkey year;

if first.gvkey then ret_cut_dummy=0;
if ret < lag(ret) then ret_cut_dummy=1;
else ret_cut_dummy=0;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @abdulla  Will this help?

 


data have;
input gvkey	year	RET;
cards;
1004	1992	-0.04762
1004	1993	0.0138
1004	1994	-0.00429
1004	1995	-0.14493
1004	1996	0.067797
1004	1997	0.107302
1004	1998	0.072464
1004	1999	0.057027 
1331	1992	0.038333
1331	1993	-0.05405
1331	1994	-0.04286
1331	1995	-0.03343
1331	1996	-0.03125
24456	1992	-0.02621
24456	1993	0.006377
24456	1994	0
24456	1995	0.05
24456	1996	0.060317
;

data want;
 set have;
 by gvkey;
 if first.gvkey then ret_cut_dummy=0;
 else ret_cut_dummy=RET<_iorc_;
 _iorc_=ret;
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Your code returns your desired result?

 

data have;
input gvkey year RET;
datalines;
1004 1992 -0.04762
1004 1993 0.0138
1004 1994 -0.00429
1004 1995 -0.14493
1004 1996 0.067797
1004 1997 0.107302
1004 1998 0.072464
1004 1999 -0.02703
1331 1992 0.038333
1331 1993 -0.05405
1331 1994 -0.04286
1331 1995 -0.03343
1331 1996 -0.03125
24456 1992 -0.02621
24456 1993 0.006377
24456 1994 0
24456 1995 0.05
24456 1996 0.060317
;

Data want;
   set have;
   by gvkey year;
   if first.gvkey then ret_cut_dummy=0;
   if ret < lag(ret) then ret_cut_dummy=1;
   else ret_cut_dummy=0;
run;
abdulla
Pyrite | Level 9
I am sorry. I have edited the data slightly. Now I am not getting what I want. If you look at the first gvkey 1331 the dummy should be 0, but I am getting 1.
novinosrin
Tourmaline | Level 20

HI @abdulla  Will this help?

 


data have;
input gvkey	year	RET;
cards;
1004	1992	-0.04762
1004	1993	0.0138
1004	1994	-0.00429
1004	1995	-0.14493
1004	1996	0.067797
1004	1997	0.107302
1004	1998	0.072464
1004	1999	0.057027 
1331	1992	0.038333
1331	1993	-0.05405
1331	1994	-0.04286
1331	1995	-0.03343
1331	1996	-0.03125
24456	1992	-0.02621
24456	1993	0.006377
24456	1994	0
24456	1995	0.05
24456	1996	0.060317
;

data want;
 set have;
 by gvkey;
 if first.gvkey then ret_cut_dummy=0;
 else ret_cut_dummy=RET<_iorc_;
 _iorc_=ret;
run;
abdulla
Pyrite | Level 9
Thanks novinosrin. It works
Shmuel
Garnet | Level 18

Using LAG function under IF may result unpredictable.

Here is a slight change to @PeterClemmensen code:

proc sort data=have; by gvkey year; run; 

Data want;
   set have;
   by gvkey ;
   retain prev_ret;
   if first.gvkey then do;
      ret_cut_dummy=0;
	  prev_ret = ret;
   end; 
   else do;
	   if ret < prev_ret then ret_cut_dummy=1;
	   else ret_cut_dummy=0;
	   prev_ret = ret;
   end;
   drop prev_ret;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 453 views
  • 1 like
  • 4 in conversation