BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Takdir
Obsidian | Level 7
data work.delete;
input firm $ year profit;
datalines;
A 2001 90
A 2002 80
A 2003 70
A 2004 90
A 2005 60
B 2001 70
B 2002 80
B 2003 90
C 2001 70
C 2002 80
C 2003 90
C 2004 60
C 2005 80
D 2001 10
D 2002 20

I wish to delete those firms that does not have observation for 5 years. My desired output is
A 2001 90
A 2002 80
A 2003 70
A 2004 90
A 2005 60
C 2001 70
C 2002 80
C 2003 90
C 2004 60
C 2005 80


1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data work.delete;
input firm $ year profit;
datalines;
A 2001 90
A 2002 80
A 2003 70
A 2004 90
A 2005 60
B 2001 70
B 2002 80
B 2003 90
C 2001 70
C 2002 80
C 2003 90
C 2004 60
C 2005 80
D 2001 10
D 2002 20
;

proc sql;
create table want as
select *
from delete
group by firm
having count(distinct year)>=5;
quit;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

This approach assumes your data is sorted by FIRM:

 

data want;

counter=0;

do until (last.firm);

   set have;

   by firm;

   counter + 1;

end;

do until (last.firm);

   set have;

   by firm;

   if counter >= 5 then output;

end;

drop counter;

run;

 

The top loop counts observations, and the bottom reads the same observations and outputs appropriately.

novinosrin
Tourmaline | Level 20
data work.delete;
input firm $ year profit;
datalines;
A 2001 90
A 2002 80
A 2003 70
A 2004 90
A 2005 60
B 2001 70
B 2002 80
B 2003 90
C 2001 70
C 2002 80
C 2003 90
C 2004 60
C 2005 80
D 2001 10
D 2002 20
;

proc sql;
create table want as
select *
from delete
group by firm
having count(distinct year)>=5;
quit;
novinosrin
Tourmaline | Level 20

data work.delete;
input firm $ year profit;
datalines;
A 2001 90
A 2002 80
A 2003 70
A 2004 90
A 2005 60
B 2001 70
B 2002 80
B 2003 90
C 2001 70
C 2002 80
C 2003 90
C 2004 60
C 2005 80
D 2001 10
D 2002 20
;
data _null_;
if _n_=1 then do;
   dcl hash H (dataset:'delete',ordered: "A", multidata:'y') ;
   h.definekey  ("firm") ;
   h.definedata (all:'y') ;
   h.definedone () ;
end;
  _n_=0;
do until(last.firm);
set delete end=lr;
by firm year;
if first.year then _n_+1;
end;
if _n_<5 then h.remove();
if lr then h.output(dataset:'want');
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 775 views
  • 0 likes
  • 3 in conversation