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;

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

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