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

Hi, I have a dataset like input below.

 

FirmID        Year        indicator

10001        1995          1

10001        1996          0

10001        1997          0

10001        1997          1

10001        1998          1

10001        1999          0

10001        1999          0

10001        1999          1

10001        2000          0

10001        2001          1

 

I would like to count how many 0 for each company in each year. The output is like below.

 

FirmID        Year         nb_Indicator_0

10001        1995                   0

10001        1996                   1

10001        1997                   1

10001        1998                   0

10001        1999                   2

10001        2000                   1

10001        2001                   0

 

what code do I need to use? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @dapenDaniel 

 

data have;
input FirmID        Year        indicator;
cards;
10001        1995          1
10001        1996          0
10001        1997          0
10001        1997          1
10001        1998          1
10001        1999          0
10001        1999          0
10001        1999          1
10001        2000          0
10001        2001          1
;

proc sql;
create table want as
select firmid, year, sum(indicator=0) as nb_Indicator_0
from have
group by firmid, year;
quit;

/*or*/

data want;
do until(last.year);
 set have;
 by FirmID Year;
 nb_Indicator_0=sum(indicator=0,nb_Indicator_0);
end;
drop indicator;
run;

 

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@dapenDaniel wrote:

Hi, I have a dataset like input below.

 

FirmID        Year        indicator

10001        1995          1

10001        1996          0

10001        1997          0

10001        1997          1

10001        1998          1

10001        1999          0

10001        1999          0

10001        1999          1

10001        2000          0

10001        2001          1

 

I would like to count how many 0 for each company in each year. The output is like below.

 

FirmID        Year         nb_Indicator_0

10001        1995                   0

10001        1996                   1

10001        1997                   1

10001        1998                   0

10001        1999                   2

10001        2000                   1

10001        2001                   0

 

what code do I need to use? Thanks!


data have2;
    set have;
    zero = (nb_indicator=0);
run;
proc summary data=have2 nway;
    class year;
    var zero;
    output out=want sum=count_zero;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @dapenDaniel 

 

data have;
input FirmID        Year        indicator;
cards;
10001        1995          1
10001        1996          0
10001        1997          0
10001        1997          1
10001        1998          1
10001        1999          0
10001        1999          0
10001        1999          1
10001        2000          0
10001        2001          1
;

proc sql;
create table want as
select firmid, year, sum(indicator=0) as nb_Indicator_0
from have
group by firmid, year;
quit;

/*or*/

data want;
do until(last.year);
 set have;
 by FirmID Year;
 nb_Indicator_0=sum(indicator=0,nb_Indicator_0);
end;
drop indicator;
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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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