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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 739 views
  • 1 like
  • 4 in conversation