BookmarkSubscribeRSS Feed
CharlesR
Calcite | Level 5
Hello,
I have the following issue:

Under certain scenarios, I have a data set where several different people should be responsible for a situation. So, if the variable

[pre] PlayResult = 'hit'[/pre]

there can be anywhere from 1 to 4 fielders responsible for this. I would then want to have a variable, "ResponsibleFielder" be able to have whatever fielders should be responsible, in other words, i would want the variable to be able to reference multiple values, where the number of values can change from data line to data line. A 3-dimensional variable for my data set if you will.

So for example, if [pre]PlayResult = 'hit'[/pre] for the first data line, i might have [pre]ResponsibleFielder= Player1, Player2, Player3[/pre] and then for the second data line have [pre]ResponsibleFielder= Player7, Player12[/pre] and for the third dataline have [pre]ResponsibleFielder= Player14[/pre]

Is this possible? Message was edited by: CharlesR
8 REPLIES 8
Reeza
Super User
Sort of. You can't directly have a variable have more than value at a time AFAIK, so you end up with a few different solutions:

1) A text variable with all values in the cell ie responsible fielder= "Player1, Player2"
2) Multiple variables ie resposiblefielder1 responsiblefielder2 etc..
3) Multiple records for each hit with an identifier to the fielder ie
responsiblefielder=player1 record=1
responsiblefielder=player2 record=2

Which method you choose depends on what you plan to do next with the field and how it will affect other fields and calculations.

HTH,
Reeza
CharlesR
Calcite | Level 5
What do you mean by multiple records? Does this create a new line of data?
Reeza
Super User
Yes, you would have multiple lines for each play result with multiple players.
Benjy
Calcite | Level 5
It sounds to me like you want to build yourself a multi-dimensional array -- perhaps something like the following:
dimension 1=inning
dimension 2=at bat (seq number of at-bat in the inning)
dimension 3=batter
dimension 4=multimensional array of defensive players involved in play
dimension 5=result

That's off the top of my head. But the problem sounds as if it is soluble by building arrays. Or hash tables instead of arrays.
CharlesR
Calcite | Level 5
Don't want to go the array route because the data set is huge, and this would be a small part of it.

What's a hash table?
Reeza
Super User
An array will just reference multiple variables and a hash table will bring info in from another table.

A hash table and array are both temporary structures, they're not ways to store your information.

Are you asking about how to create a table to store your information or using it?

Like I mentioned before the optimal way to store the data depends on how you'd like to use it.
CharlesR
Calcite | Level 5
I was looking for a data set to store it. I'm thinking the 2nd suggestion you made, using multiple variables to show each fielder is going to be the way to go.

So that leads to a new question: how do i take each of the results in the fieldingbyposition column and transpose them to new variables. I'm going to post it as a new thread in this same section.

Here's the example: [pre]
z BT BBV DP Bat Res field
23 5 1 N R Hit 0
23 5 1 N R Out 5
23 5 1 Y L Hit 0
23 5 1 Y L Out 5
23 5 1 Y L Out 9
[/pre]

The first two line of data show one scenario with 2 different fielders, 0 & 5. The next 3 lines show a different scenario with 3 fielders, 0, 5 & 9. I then want it to look like this:
[pre]
z BT BBV DP Bat Res field1 field2 field3
23 5 1 N R Hit 0 5
23 5 1 Y L Hit 0 5 9
[/pre]
What do i need to do to make this work?
Ksharp
Super User
OK.That is easy.


[pre]
data temp;
input z BT BBV DP $ Bat $ Res $ field ;
cards;
23 5 1 N R Hit 0
23 5 1 N R Out 5
23 5 1 Y L Hit 0
23 5 1 Y L Out 5
23 5 1 Y L Out 9
;
run;
data temp;
set temp;
if res = 'Hit' then count+1;
run;

data want;
set temp;
by count ;
length fieldres $ 200;
retain fieldres num;
nobs+1;
if first.count then do;
call missing (fieldres);
num=nobs;
end;
fieldres=catx(' ',fieldres,field);
if last.count then do;
set temp point=num;
output;
end;
drop field nobs count;
run;
[/pre]



Ksharp

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 3793 views
  • 0 likes
  • 4 in conversation