So I have some data which has different players assigned to different clusters, along with a do loop for each one of the clusters which is stored in the macro language. So basically, right now, I have a do loop which says "do the following from 1 to C" where C is the macro number (ranges from 1 to 8.)
Then I want to add up the number of times a player appears in the particular cluster, and also how many times they appear overall. So the following code is a basis. I cant just look at the observations in C because then I wouldnt know how many times a player would appear total. I dont know how to use a first. in a similar fashion when the cluster number will change (the variable cluster will vary for each player. So lets say when a player is at home, he is in cluster 4, and when he goes on the road he might appear in cluster 5 and so on. So I cant actually use the code as it is now).
Does this make sense? Any ideas?
data megasetfinallast_&&K&&C; | |||
set megasetfinal1; | |||
by player_id cluster game_date; | |||
if first.cluster then numcluster=0; | |||
if first.player_id then numtotal=0; | |||
numtotal+1; | |||
numcluster+1; | |||
retain numcluster; | |||
if last.player_id | |||
if numcluster>25 | |||
run; |
Does this make sense? Any ideas?
No.
Show example data using the have/need scenario.
Need:
Cluster | Player_ID | Game_date | numcluster | numtotal |
---|---|---|---|---|
2 | 111 | 04/05/12 | 1 | 1 |
2 | 111 | 04/06/12 | 2 | 2 |
3 | 111 | 04/07/12 | 2 | 3 |
3 | 111 | 04/08/12 | 2 | 4 |
2 | 111 | 04/09/12 | 3 | 5 |
2 | 111 | 04/10/12 | 4 | 6 |
4 | 111 | 04/11/12 | 4 | 7 |
And so forth.So lets say for cluster 2, I only want the player for the obs. in cluster 2, the last observation would have numcluster to be 4 and numtotal to be 7. Numcluster will only increase when cluster = C (the particular iteration of the do loop.) so at C=1, numcluster gets added by 1.
I am trying to get it to do if cluster=&&C then numcluster+1 but it doesnt seem to work.
I should add the do loop looks like this:
%do C=1 %to &Num_Clusters;
and then Num_Clusters is found in the macro call with a value of 8.
Not clear at all.
On the one hand it sounds like you want to GENERATE data based on some macro variable. If that is the case write a date step. You should be able to write the data step using data step DO looping and just set the bounds of the loops using the macro variables.
data want;
do c= 1 to &num_clusters;
......
output;
end;
run;
On the other hand it sounds like you want to summarize some existing data and group by variables in the data. In that case use PROC SUMMARY or possible PROC TABULATE.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.