DATA Step, Macro, Functions and more

Using first. and last. with macro elements

Reply
Frequent Contributor
Posts: 82

Using first. and last. with macro elements

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;
Respected Advisor
Posts: 3,799

Re: Using first. and last. with macro elements

Does this make sense? Any ideas?

No.

Show example data using the have/need scenario.

Frequent Contributor
Posts: 82

Re: Using first. and last. with macro elements

Posted in reply to data_null__

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.

Frequent Contributor
Posts: 82

Re: Using first. and last. with macro elements

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.

Super User
Super User
Posts: 7,042

Re: Using first. and last. with macro elements

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.

Ask a Question
Discussion stats
  • 4 replies
  • 260 views
  • 0 likes
  • 3 in conversation