BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

May I know how to calculate the sum for team1 with finish le 3 for team1, and finish le 5 for team2 (as stated in below code) for both temp_cl columns?

 

DATA TEST;
INPUT ID TEAM $ RANK $ CL1 CL2 FINISH;
DATALINES;
120 T1 A1 0 0 1
121 T1 A2 20 30 1
122 T1 A2 30 50 2
123 T1 A2 40 60 3
124 T1 A2 50 40 4
125 T1 A2 60 30 5
220 T2 A1 0 0 1
221 T2 A2 24 130 1
222 T2 A2 34 150 2
223 T2 A2 44 160 3
224 T2 A2 54 140 4
225 T2 A2 64 130 5
;
RUN;

PROC SQL;
CREATE TABLE TEST1 AS SELECT *,
COALESCE((SELECT SUM(CL1) FROM TEST WHERE TEAM=A.TEAM AND FINISH LE 5),CL1)
AS TEMP_CL1,
COALESCE((SELECT SUM(CL2) FROM TEST WHERE TEAM=A.TEAM AND FINISH LE 5),CL2)
AS TEMP_CL2
FROM TEST A;
QUIT

2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @scb 

 

Does this code meet your expectations?

Best,

proc sql;
	create table test1 as
	
 	select a.*, b.TEMP_CL1, b.TEMP_CL2
 	from test as a inner join
 		 (select distinct team,
 		 		 sum(CL1) as TEMP_CL1,
 		 		 sum(CL2) as TEMP_CL2
 		 from test
 		 where team="T1" and finish le 2) as b
 	on a.team=b.team
 	
 	union all corr
 	
 	select a.*, b.TEMP_CL1, b.TEMP_CL2
 	from test as a inner join
 		 (select distinct team,
 		 		 sum(CL1) as TEMP_CL1,
 		 		 sum(CL2) as TEMP_CL2
 		 from test
 		 where team="T2" and finish le 5) as b
 	on a.team=b.team;
run;

 

Capture d’écran 2020-05-02 à 14.59.13.png

Kurt_Bremser
Super User

Please stop shouting at the poor SAS interpreter, it hasn't done anything to deserve this.

Try this:

proc sql;
create table want as
  select
    team,
    sum (case
      when team = 'T1' and finish le 3 or team = 'T2' and finish le 5
      then cl1
      else 0
      end
    ) as temp_cl1,
    sum (case
      when team = 'T1' and finish le 3 or team = 'T2' and finish le 5
      then cl2
      else 0
      end
    ) as temp_cl2
  from have
  group by team
;
quit;

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