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

Hello, 

 

I have a dataset like below.

data have;
input firmid $ Year indicator value;
datalines;
1001 1991  1  2
1001 1991  0  3
1001 1991  0  4
1001 1991  1  5
1002 2005  1  11
1002 2005  1  10
1002 2006  0  13
1002 2006  1  14
1002 2007  1  20
1002 2007  1  5
1002 2007  0  3
;
run;

I would like to calculate the sum for indicator is 1 and sum for indicator is 0 by firmid, year.

 

The expected output is below. 

data want;
input firmid $ Year value1 value0;
datalines;
1001 1991  7  7
1002 2005  21  0
1002 2006  14  13
1002 2007  25  3
;
run;

What code do I need to use? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21
proc sql;
create table want as
select
    firmId,
    year,
    sum(case when indicator=1 then value else 0 end) as value1,    
    sum(case when indicator=0 then value else 0 end) as value0
from have
group by firmId, year;
quit;
PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21
proc sql;
create table want as
select
    firmId,
    year,
    sum(case when indicator=1 then value else 0 end) as value1,    
    sum(case when indicator=0 then value else 0 end) as value0
from have
group by firmId, year;
quit;
PG
dapenDaniel
Obsidian | Level 7

Hi @PGStats ! Thanks for your help. 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1063 views
  • 1 like
  • 2 in conversation