DATA Step, Macro, Functions and more

Multiple values for a "variable"

Reply
Contributor
Posts: 58

Multiple values for a "variable"

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
Super User
Posts: 19,860

Re: Multiple values for a "variable"

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
Contributor
Posts: 58

Re: Multiple values for a "variable"

What do you mean by multiple records? Does this create a new line of data?
Super User
Posts: 19,860

Re: Multiple values for a "variable"

Yes, you would have multiple lines for each play result with multiple players.
New Contributor
Posts: 3

Re: Multiple values for a "variable"

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.
Contributor
Posts: 58

Re: Multiple values for a "variable"

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?
Super User
Posts: 19,860

Re: Multiple values for a "variable"

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.
Contributor
Posts: 58

Re: Multiple values for a "variable"

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?
Super User
Posts: 10,044

Re: Multiple values for a "variable"

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
Ask a Question
Discussion stats
  • 8 replies
  • 812 views
  • 0 likes
  • 4 in conversation