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

Dear Madam/Sir,

I would like to perform one operation by firm (gvkey) in dataset. Below is as-is dataset for only one firm and I like to drop observations after lead5 (from 2012) for every firm in the dataset.

Do loop example mostly use array. In this case, I cannot use array. Any help will be highly appreciated. Thank you.

 

data want; set have;
do over gvkey
if cyear gt lead5 then delete;
end;
run;

 

gvkey cyear lead1 lead2 lead3 lead4 lead5
1161200000000
1161200100000
1161200200000
1161200300000
1161200400000
1161200500000
1161200600000
1161200710000
1161200801000
1161200900100
1161201000010
1161201100001
1161201200000
1161201300000
1161201400000
1161201500000
1161201600000
1161201700000
1161201800000
1161201900000
1161202000000
1161202100000

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    retain flag_year flag;
    by gvkey;
    if first.gvkey then flag=0;
    if lead5=1 then do;
        flag=1;
        flag_year=cyear;
    end;
    if flag=1 and cyear>flag_year then delete;
    drop flag_year flag;
run;

 

Please be kind to us. If you want a solution that works for more than 1 gvkey, from now provide data with more than 1 gvkey.

 

I think you misunderstand arrays. Arrays are not used to work row by row, or gvkey by gvkey, as your request would require. Arrays work column by column in each row, which is not what you need for this problem.

--
Paige Miller

View solution in original post

5 REPLIES 5
joon1
Quartz | Level 8

Thank you so much.

PaigeMiller
Diamond | Level 26
data want;
    set have;
    retain flag_year flag;
    by gvkey;
    if first.gvkey then flag=0;
    if lead5=1 then do;
        flag=1;
        flag_year=cyear;
    end;
    if flag=1 and cyear>flag_year then delete;
    drop flag_year flag;
run;

 

Please be kind to us. If you want a solution that works for more than 1 gvkey, from now provide data with more than 1 gvkey.

 

I think you misunderstand arrays. Arrays are not used to work row by row, or gvkey by gvkey, as your request would require. Arrays work column by column in each row, which is not what you need for this problem.

--
Paige Miller
joon1
Quartz | Level 8

Thank you so much. Your code perfectly works.

mkeintz
PROC Star

If you have exactly one lead5=1 year per gvkey, then:

 

data want (drop=_:);
  merge have (where=(lead5=1) rename=(cyear=_cyear5))
        have ;
  by gvkey;
  if cyear<=_cyear5;
run;

If a gvkey has no lead5 years, then the above will drop all observations.  But if you actually WANT all observations in the absence of a lead5 record, then change the subsetting IF to

  if cyear<=_cyear5  or _cyear5=.;

If you have multiple lead5=1 years, then the above will keep all records up through the last lead5 year (assuming data are sorted by cyear within gvkey).

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 501 views
  • 1 like
  • 4 in conversation