SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

MaCRO

Reply
Occasional Contributor
Posts: 12

MaCRO

[ Edited ]

Hello, I have 3 excel data sets named data 1,data 2 and data 3 with common variable id. I have imported them using MACRO. I have merged (data 1 ,data 2 and data 3 data 2) in the following way,

 

%macro merge (factor,m,n);

data &factor;

merge %do i = &m %to &n;

data&i

%end;

;

by id;

run;

proc print data = &factor;

run;

%mend merge;

%merge (combine1, 1,2);

%merge (combine2, 2,3);

 

Now, I need to solve this

 

1. merge data 1 and data 2 so that I have records only for those values of ID that are in data 1.

2. merge all three files so that I have records only for those values of ID that are in all 3 datasets.

 

How can I write the SAS MACRO code for these?

Contributor
Posts: 20

Re: MaCRO

[ Edited ]
Posted in reply to docsaurmi

You should use the IN option:

 

https://onlinecourses.science.psu.edu/stat481/node/18

 

Occasional Contributor
Posts: 12

Re: MaCRO

How can I use 'in' option in MACRO code?

 

Super User
Posts: 19,772

Re: MaCRO

Posted in reply to docsaurmi
Why are you doing this via a macro? It seems like a single/simple data step would work?
Occasional Contributor
Posts: 12

Re: MaCRO

I am just trying to use MACRO in this case.

Respected Advisor
Posts: 4,173

Re: MaCRO

Posted in reply to docsaurmi

Below should work.

%macro merge (factor,m,n);

  data &factor;
    merge 
      %do i = &m %to &n;
        data&i (in=in&i)
      %end;
      ;
    by id;

    %do i = &m %to &n;
      if in&i;
    %end;

  run;

  proc print data = &factor;
  run;

%mend merge;

%merge (combine1, 1,2);
%merge (combine2, 2,3);
Super User
Posts: 5,498

Re: MaCRO

Posted in reply to docsaurmi

This is actually a good exercise to use, to learn more about writing macros.

 

First, this line in your macro will change:

 

data&i

 

Instead, it will add the IN= variables:

 

data&i (in=in&i)

 

The other piece that changes is a bit more complex.  It will add a statement after the BY statement, to subset observations.  It will genereate something like this:

 

if in1 and in2 and in3;

 

To do that, all of this code goes between the BY statement and the RUN statement:

 

if

%do i=&m to &n;

in&i

%if &i < &n %then and;

%end;

;

 

Try to match up the pieces of the macro version to the pieces of the hard-coded IF statement.

 

Good luck.

Ask a Question
Discussion stats
  • 6 replies
  • 467 views
  • 0 likes
  • 5 in conversation