BookmarkSubscribeRSS Feed
namrata
Fluorite | Level 6

Hi all

I need to create a count variable that contains the total number of firm years for each firm in a panel data.

year   permno  firm

1          a          x

2          a          x

3          a          x

4          a          x

1          b          y         

2          b          y    

3          b          y

Now I need to create a count variable that gives me:

year   permno  firm      count

1          a          x          4

2          a          x          4

3          a          x          4

4          a          x          4

1          b          y          3

2          b          y          3

3          b          y          3

Thus, I can delete all firms that have less than say 3 years of observations.

The code till now is:

data want;

set have;

by permno;

if first.permno then count =1;

else count+1;

run;

this gives me the total on the last count row .I do not know how to create directly a variable that gives me the total on each row for each firm.

3 REPLIES 3
Ksharp
Super User

Easy.

data have;
input year   permno $ firm $;
datalines;
1          a          x
2          a          x
3          a          x
4          a          x
1          b          y         
2          b          y    
3          b          y
;
run;
proc sql;
create table want as
 select *,count(*) as count
  from have
   group by firm;
quit;

Ksharp

Haikuo
Onyx | Level 15

Adding one statement to Ksharp's code will give you one stone, two birds.

proc sql;

create table want as

select *,count(*) as count

  from have

   group by firm  having calculated count gt 3

;

quit;

Regards,

Haikuo

namrata
Fluorite | Level 6

Thanks a lot!The suggestions worked.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 9746 views
  • 0 likes
  • 3 in conversation