BookmarkSubscribeRSS Feed
AaronRogers
Calcite | Level 5

Hello guys,

I'm having an awful time trying to come up with how a particular code is to be written.

Below is the basis of what I have now:

View image: img1

When that code is run, it looks like this:

View image: img2

As you can see, basically it's a layout of all 2014 world cup games (The image is cropped short, there are actually 64 observations).

What I need to do within a DATA step is combine all distinct team observations (whether they be listed as Team1_Name or Team2_Name) and total their score in a new variable called 'GF'.

For example, in image two 'Spain' is listed in observations 3 and 33. In observation 3, Spain scored 1 goal, in observation 33, Spain scored 3 goals. If those were the only two observations in which Spain appeared, it's output would look like:

Team_Name, GF

Spain, 4

Here is an image I put together using data lines to try and help imagine it (the observations are limited and the goal numbers made up):

View image: img3

The code probably has to be some complicated combination of IF, THEN, DO, SELECT, WHERE... I have no idea, and I've been banging my head against the wall.

Any suggestions would be greatly appreciated.

3 REPLIES 3
AaronRogers
Calcite | Level 5

Anyone? Even a suggestion to get the ball rolling would be helpful.

AskoLötjönen
Quartz | Level 8

You didn't include data, so I just picked the result from A and B from wiki, but this should work with also with whole data:

attrib date informat=date9. format=date9.;

input Date  Team1_name $ 11-22 Team1_Score 24 Team2_Name $26-36 Team2_Score 38 Venue $ 40-55;

cards;

12Jun2014 Brazil       3 Croatia     1 Sao Paulo

13Jun2014 Mexico       1 Cameroon    0 Natal

17Jun2014 Brazil       0 Mexico         0 Fortaleza

18Jun2014 Cameroon     0 Croatia     4 Manaus

23Jun2014 Cameroon     1 Brazil      4 Brasilia

23Jun2014 Croatia      1 Mexico         3 Recife

13Jun2014 Spain        1 Netherlands 5 Salvador

13Jun2014 Chile        3 Australia   1 Cuiaba

18Jun2014 Australia    2 Netherlands 3 Porto Alegre

18Jun2014 Spain        0 Chile       2 Rio de Janeiro

23Jun2014 Australia    0 Spain       3 Curitiba

23Jun2014 Netherlands  2 Chile       0 Sao Paulo

;

proc sql;

  create table countries as

  select distinct Team1_name

  from WC2014A;

quit;

proc sql;

  select distinct Team1_name into :Country_list separated by ' '

  from WC2014A;

quit;

data _null_;

  set countries;

  call execute ('proc sql; create table '||Team1_name||' as select Team_name, Sum(GF) as GF from (select Team1_Name as Team_name, Team1_score as GF from WC2014A where Team1_name ="'||strip(Team1_name)||'" union select Team2_Name as Team_name, Team2_score as GF from WC2014A where Team2_name ="'||strip(Team1_name)||'") tmp group by Team_name;quit;');

run;

data W2014B;

  set &Country_list;

run;

ChrisNZ
Tourmaline | Level 20

All in one data step:

data HAVE(index=(Team1_name Team2_name));

input Date $9 Team1_name $ 11-22 Team1_Score 24 Team2_Name $26-36 Team2_Score 38 Venue $ 40-55;

cards;

12Jun2014 Brazil 3 Croatia     1 Sao Paulo

13Jun2014 Mexico 1 Cameroon    0 Natal

17Jun2014 Brazil 0 Mexico      0 Fortaleza

18Jun2014 Cameroon 0 Croatia     4 Manaus

23Jun2014 Cameroon 1 Brazil      4 Brasilia

23Jun2014 Croatia 1 Mexico      3 Recife

13Jun2014 Spain 1 Netherlands 5 Salvador

13Jun2014 Chile        3 Australia   1 Cuiaba

18Jun2014 Australia 2 Netherlands 3 Porto Alegre

18Jun2014 Spain        0 Chile       2 Rio de Janeiro

23Jun2014 Australia 0 Spain       3 Curitiba

23Jun2014 Netherlands 2 Chile       0 Sao Paulo

run;

data ALL;

  set HAVE(keep= Team1_name Team1_Score rename=(Team1_name=TEAM Team1_Score=SCORE))

     HAVE(keep= Team2_name Team2_Score rename=(Team2_name=TEAM Team2_Score=SCORE)) ;

  by TEAM;

  if first.TEAM then TOTAL=0;

  TOTAL+SCORE;

  if last.TEAM then output;

  keep TEAM TOTAL;

run;

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!

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.

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