<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dynamic macro-merge by one or two vars in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819074#M323326</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro myJoin(list1, list2);

%local a b c d;

%let a = %scan(&amp;amp;list1.,  1);
%let b = %scan(&amp;amp;list1., -1);

%let c = %scan(&amp;amp;list2.,  1);
%let d = %scan(&amp;amp;list2., -1);

proc sql;
select t1.age, t2.name
from 
sashelp.class as t1
join
sashelp.class as t2
on
t1.&amp;amp;a. = t2.&amp;amp;c.
and
t1.&amp;amp;b. = t2.&amp;amp;d.
;
run;

%mend;


options mprint;
%myJoin(name, name)

%myJoin(name age, name age)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Sun, 19 Jun 2022 20:38:42 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2022-06-19T20:38:42Z</dc:date>
    <item>
      <title>Dynamic macro-merge by one or two vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819072#M323325</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;With continue to my previous question:&lt;/P&gt;
&lt;P&gt;I need to perform merge that can by one variable or two variables (depends on number of vars in the macro).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;How to change the following code lines to dynamic code that is taking the variables in List1 macro&lt;/P&gt;
&lt;PRE&gt;on a.Cylinders=b.Cylinders
and a.Horsepower=b.Horsepower&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro BBB(mon,sourceTbl,TargetTbl,list1=,list2=);
%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table New_tbl as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.),
%end;
  count(*) as nr&amp;amp;mon. format=comma21.,
  calculated nr&amp;amp;mon./(select count(*) as total from  &amp;amp;sourceTbl.) as PCT&amp;amp;mon. format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  calculated %scan(&amp;amp;list1.,&amp;amp;j.)
  %end;
%end;
;
quit;


%MACRO RRR1;
%if %sysfunc(exist(&amp;amp;TargetTbl.)) %then %do;
proc sql ;
create table New as
select  a.*,b.nr&amp;amp;mon.,b.PCT&amp;amp;mon.
from &amp;amp;TargetTbl. as a
left join New_tbl as b
on a.Cylinders=b.Cylinders
and a.Horsepower=b.Horsepower
/***Here I want to have a dynamic problem that is taking the variables in List1***/
;
quit;
%end; 

%else %do;
Data &amp;amp;TargetTbl.;
set New_tbl;
Run;
%end; 
%MEND RRR1;
%RRR1;
%mend BBB;
  
/*Distribution by grouped Cylinders+grouped Horsepower***/
/*Distribution by grouped Cylinders+grouped Horsepower***/
/*Distribution by grouped Cylinders+grouped Horsepower***/
proc format;
value F1mt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
proc format;
value F2mt
.='Uknown'
low-80='0-80'
80-120='80-120'
120-180='120-180'
180-high='180+'
;
Run;
%BBB(mon=2204,
sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Cylinders Horsepower,
list2=F1mt  F2mt)
  

Data cars2205;/***YYMM**/
input Cylinders Horsepower;
cards;
4 110
4 90
5 120
6 185
4 130
7 250
4 130
5 190
4 76
4 91
8 410
;
run;


%BBB(mon=2205,
sourceTbl=cars2205,
TargetTbl=wanted,
list1=Cylinders Horsepower,
list2=F1mt  F2mt)
 
 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jun 2022 20:16:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819072#M323325</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T20:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro-merge by one or two vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819074#M323326</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro myJoin(list1, list2);

%local a b c d;

%let a = %scan(&amp;amp;list1.,  1);
%let b = %scan(&amp;amp;list1., -1);

%let c = %scan(&amp;amp;list2.,  1);
%let d = %scan(&amp;amp;list2., -1);

proc sql;
select t1.age, t2.name
from 
sashelp.class as t1
join
sashelp.class as t2
on
t1.&amp;amp;a. = t2.&amp;amp;c.
and
t1.&amp;amp;b. = t2.&amp;amp;d.
;
run;

%mend;


options mprint;
%myJoin(name, name)

%myJoin(name age, name age)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 20:38:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819074#M323326</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-06-19T20:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro-merge by one or two vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819075#M323327</link>
      <description>May you please explain your code ?&lt;BR /&gt;Why did you write minus 1 and plus 1 in scan function?&lt;BR /&gt;Will this technique  work well when there will be only one variable  to match in merge ?</description>
      <pubDate>Sun, 19 Jun 2022 21:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819075#M323327</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T21:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro-merge by one or two vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819089#M323332</link>
      <description>&lt;P&gt;Great, very clever solution.&lt;/P&gt;
&lt;P&gt;As I understand "1"&amp;nbsp; takes the first element and "-1" takes the last.&lt;/P&gt;
&lt;P&gt;If there is one element than first and last are same.&lt;/P&gt;
&lt;P&gt;If there are 2 elements then "1" takes the first and&amp;nbsp; "-1" takes the last.&lt;/P&gt;
&lt;P&gt;The only thing need to modify is that need only 2 macro vars a,b because list1 is the explanatory variables and list2 is the correspondence&amp;nbsp; formats.&lt;/P&gt;
&lt;P&gt;Here is the solution that was made thanks to your advice&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro BBB(mon,sourceTbl,TargetTbl,list1=,list2=);

%local a b nr_vars;
%let a = %scan(&amp;amp;list1.,  1);
%let b = %scan(&amp;amp;list1., -1);
 
%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table New_tbl as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.),
%end;
  count(*) as nr&amp;amp;mon. format=comma21.,
  calculated nr&amp;amp;mon./(select count(*) as total from  &amp;amp;sourceTbl.) as PCT&amp;amp;mon. format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  calculated %scan(&amp;amp;list1.,&amp;amp;j.)
  %end;
%end;
;
quit;


%MACRO RRR1;
%if %sysfunc(exist(&amp;amp;TargetTbl.)) %then %do;
proc sql ;
create table New as
select  t1.*,
coalesce(t2.nr&amp;amp;mon.,0 )  as nr&amp;amp;mon. format=comma21.,
coalesce(t2.PCT&amp;amp;mon.,0 ) as PCT&amp;amp;mon. format=percent8.1
from &amp;amp;TargetTbl. as t1
left join New_tbl as t2
on 
t1.&amp;amp;a. = t2.&amp;amp;a.
and
t1.&amp;amp;b. = t2.&amp;amp;b.
/***Here I want to have a dynamic problem that is taking the variables in List1***/
;
quit;
%end; 

%else %do;
Data &amp;amp;TargetTbl.;
set New_tbl;
Run;
%end; 
%MEND RRR1;
%RRR1;
%mend BBB;
  
/*Distribution by grouped Cylinders+grouped Horsepower***/
/*Distribution by grouped Cylinders+grouped Horsepower***/
/*Distribution by grouped Cylinders+grouped Horsepower***/
proc format;
value F1mt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
proc format;
value F2mt
.='Uknown'
low-80='0-80'
80-120='80-120'
120-180='120-180'
180-high='180+'
;
Run;
%BBB(mon=2204,
sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Cylinders Horsepower,
list2=F1mt  F2mt)
  

Data cars2205;/***YYMM**/
input Cylinders Horsepower;
cards;
4 110
4 90
5 120
6 185
4 130
7 250
4 130
5 190
4 76
4 91
8 410
;
run;


%BBB(mon=2205,
sourceTbl=cars2205,
TargetTbl=wanted,
list1=Cylinders Horsepower,
list2=F1mt  F2mt)
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jun 2022 04:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-merge-by-one-or-two-vars/m-p/819089#M323332</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-20T04:50:58Z</dc:date>
    </item>
  </channel>
</rss>

