BookmarkSubscribeRSS Feed
AaronRogers
Calcite | Level 5

Hello,

I'm trying to come up with some method to efficiently organize some data to get some particular results. Below is some sample data:

================================================================

DATA WORK.INFO;

INPUT Team1_Name $ 1-9 Team1_Score 11 Team2_Name $ 13-27 Team2_Score 28 Game_Type $ 30-50 Venue $ 52-62;

DATALINES;

Spain     1 Italy          0 Friendly              Madrid

Aruba     2 Guam           2 Friendly              Oranjestad

Namibia   1 Congo          0 Continental Qualifier Windhoek

Spain     2 Bolivia        0 Friendly              Seville

Australia 0 Spain          3 FIFA World Cup Final  Curitiba

France    1 Spain          0 Friendly              Paris

Ethiopia  1 Algeria        2 Continental Qualifier Addis Ababa

Spain     5 Venezuela      0 Friendly              Malaga

Nepal     0 Maldives       1 Continental Qualifier Kathmandu

Spain     2 Serbia         0 Friendly              St. Gall

Spain     4 Korea Republic 1 Friendly              Berne

;

RUN;

DATA WORK.STUFF;

SET WORK.INFO;

/** ALL ORGANZATION GOES HERE AND NO WHERE ELSE. **/

/** SO, PROC SQL IS OFF THE TABLE. **/

RUN;

================================================================

I'm trying to create a new data set (WORK.STUFF) using only statements within the data step that adheres to the following conditions.

1. The data set must only hold observations including "Spain" (Can be observed in Team1_Name and Team2_Name variables).

2. The data set must have only one observation with two variables ("S_Goals" and "O_Goals" -- Spain goals and opponent goals).

     "S_Goals" is the average number of goals scored by Spain per game. "O_Goals" is the average number of goals scored by the opponents of Spain per game.

Anyone have any clue? The only thing I can come up with this splitting the data where Spain is either Team1_Name or Team2_Name and then interleaving them into a single data set. The goal is to get this result and adhere to the above guidelines.

If anyone has a clue, I'd appreciate it.

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Is this some sort of exam you have to pass ?

In which case you'd better find out by yourself or you'll never learn.

data WORK.STUFF;

  set WORK.INFO end=LASTOBS;

  N+1;

  if Team1_Name='Spain' then do;

    S_Goals+Team1_Score;

    O_Goals+Team2_Score;

  end;

  else if Team2_Name='Spain' then do;

    S_Goals+Team2_Score;

    O_Goals+Team1_Score;

  end;

  else N+-1;

  if LASTOBS then do;

    S_Goals=S_Goals/N;

    O_Goals=O_Goals/N;

      output;

  end;

  keep O_Goals S_Goals;

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
  • 1 reply
  • 735 views
  • 0 likes
  • 2 in conversation