BookmarkSubscribeRSS Feed
Xamius32
Calcite | Level 5

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;
4 REPLIES 4
data_null__
Jade | Level 19

Does this make sense? Any ideas?

No.

Show example data using the have/need scenario.

Xamius32
Calcite | Level 5

Need:

ClusterPlayer_IDGame_datenumclusternumtotal
211104/05/1211
211104/06/1222
311104/07/1223
311104/08/1224
211104/09/1235
211104/10/1246
411104/11/1247

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.

Xamius32
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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