BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MB_Analyst
Obsidian | Level 7

I have a number of data files that I want to count observations per group for, in addition to removing id's with only one observation.

 

To do this with a single data file I can use:

 

DATA want;
    SET have;
    BY id date;
    IF first.id THEN count=0; 
    count+1;
   IF first.id AND last.id THEN DELETE;
RUN;

Is there anyway I can set this up as a macro to run it on 5+ data files?

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Untested:

 

%macro MACRO_NAME(Have=, Want=);
data &want.;
   set &have.;
   by id date;

   if first.id then
      count=0;
   count+1;

   if first.id and last.id then
      delete;
run;
%mend;

%MACRO_NAME(Have= have, Want= want)

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

Untested:

 

%macro MACRO_NAME(Have=, Want=);
data &want.;
   set &have.;
   by id date;

   if first.id then
      count=0;
   count+1;

   if first.id and last.id then
      delete;
run;
%mend;

%MACRO_NAME(Have= have, Want= want)
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You don't need macro, you can simply combine the datasets you want to check, then process them using by groups.  It will be faster and use less resources.  Post some test data for an exact example but:

data want;
  set dataset1 dataset2 dataset3... indsname=tmp;
  dsname=tmp;
run;
data want;
  set want;
  by dsname id;
  if first.id then count=0;
  count=sum(count,1);
  if first.id and last.id then delete;
run;

  

data_null__
Jade | Level 19

@RW9 wrote:

You don't need macro, you can simply combine the datasets you want to check, then process them using by groups.  It will be faster and use less resources.  Post some test data for an exact example but:

data want;
  set dataset1 dataset2 dataset3... indsname=tmp;
  dsname=tmp;
run;
data want;
  set want;
  by dsname id;
  if first.id then count=0;
  count=sum(count,1);
  if first.id and last.id then delete;
run;

  


That gets messy if the only common variable(s) are the ID variables.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

True, but then you would assume that if the process is that similar for each of these datasets either - its data split out before, data that can be set together retaining on necessary variables, or data from which the process of counting id could be extracted from the data and then re-merged.  Those scenarios are pretty simple, if its more complicated that that, maybe assess the steps before.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 911 views
  • 1 like
  • 4 in conversation