BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10


data t1; input id; cards; 1 2 3 4 5 ; run; data t2; input id; cards; 5 6 7 8 ; run; %macro left(left,right ,var); proc sql; select * from &left as a left join &right as b on a.&var=b.&var; quit; %mend; %left(t1,t2,id);

Here  I am trying to join table with macro application 

how to define joins for 

left join

right join

full join

inner join

need help me 

above code is correct 

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Why do you want to do this? The SAS macro language is for generating code. Why not just write the joins with no macro?

 

And if this is for practice, why do you want the answer rather than trying yourself?

BrahmanandaRao
Lapis Lazuli | Level 10
i tried but how to use left ,right inner joins using macro application see above code
PaigeMiller
Diamond | Level 26

@BrahmanandaRao wrote:

Here  I am trying to join table with macro application 

how to define joins for 

left join

right join

full join

inner join

need help me 

above code is correct 


Is the question that you want the code to do something other than a left join?

 

You can simply add another argument into the macro definition that will specify the type of join.

--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10
data t1;
input id;
cards;

1
2
3
4
5
;
run;

data t2;
input id;
cards;
5
6
7
8
;
run;



%macro abc(left,right ,var,j_type);
proc sql;
select *
from &left as a
&j_type  &right as b 
on a.&var=b.&var;
quit;
%mend;

%abc(t1,t2,id,right_join);

Anandkvn_0-1606738225610.png

 

 

 

 

 

 

PaigeMiller
Diamond | Level 26

Your macro, when all the macro variables resolve, must produce working valid legal SAS code — and it does not produce working valid legal SAS code. That's what the log is showing you. A very trivial change fixes this.

 

If it's still not obvious, then you need to go back to the first step in macro writing, which is to produce code that works (in this case for your right join) without macros and without macro variables. Once you have this working, you should be able to turn it into a macro that works. If you can't produce working SAS code without macros and without macro variables, then it will never work when you add macros and add macro variables.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 6 replies
  • 747 views
  • 0 likes
  • 4 in conversation