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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1148 views
  • 0 likes
  • 3 in conversation