<?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: join tables with &amp;quot;lag&amp;quot; in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/join-tables-with-quot-lag-quot/m-p/563638#M10917</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the solution&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input CART $	INI_3	MED_1	FIN_2;
datalines;
A 1.50 2.90 4.30
B 1.30 2.40 4.80
;
run;

data table2;
input REF	INI	MED	FIN;
datalines;
1 1.44 9.50 5.76
2 3.70 7.80 6.80
3 5.96 6.10 7.84
4 8.22 4.40 8.88
5 10.48 2.70 9.92
6 12.74 1.00 10.96
7 4.70 6.98 11.00
8 17.26 12.96 11.04
9 29.82 18.94 11.08
10 42.38 24.92 11.12
;
run;

proc sql;
create table test as
select a.*,b.* from table1 as a , table2 as b;
quit;

options minoperator mindelimiter=',';
%macro calculation(outdataset=,ref_tab1=,ref_i_3=,ref_m_1=,ref_f_2=,finaldata=);
proc sql;
create table &amp;amp;outdataset as
select a.cart,a.ini_3,a.FIN_2,a.MED_1,b.FIN,b.MED,b.ini,b.ref as ref_b,a.ref from
test(where=(ref eq &amp;amp;ref_tab1)) as a , test (where=(ref in (&amp;amp;ref_i_3.,&amp;amp;ref_m_1.,&amp;amp;ref_f_2.))) as b where a.cart=b.cart ;
quit; 

proc sql;
create table &amp;amp;finaldata as
select cart,sum(output1) as output from
(select cart,sum(ind1,med1,fin2) as output1 from (
select cart,
case when ref eq &amp;amp;ref_tab1. and ref_b eq &amp;amp;ref_i_3. then ini_3*ini else . end as ind1,
case when ref eq &amp;amp;ref_tab1. and ref_b eq &amp;amp;ref_m_1. then MED_1*MED else . end as MED1,
case when ref eq &amp;amp;ref_tab1. and ref_b eq &amp;amp;ref_f_2. then FIN_2*FIN else . end as fin2 from &amp;amp;outdataset)group by cart)group by cart;
quit;

%mend;


%calculation(outdataset=tab1,ref_tab1=1,ref_i_3=4,ref_m_1=2,ref_f_2=3,finaldata=_want1);
%calculation(outdataset=tab1,ref_tab1=2,ref_i_3=5,ref_m_1=3,ref_f_2=4,finaldata=_want2);
%calculation(outdataset=tab1,ref_tab1=3,ref_i_3=6,ref_m_1=4,ref_f_2=5,finaldata=_want3);
%calculation(outdataset=tab1,ref_tab1=4,ref_i_3=7,ref_m_1=5,ref_f_2=6,finaldata=_want4);
%calculation(outdataset=tab1,ref_tab1=5,ref_i_3=8,ref_m_1=6,ref_f_2=7,finaldata=_want5);
%calculation(outdataset=tab1,ref_tab1=6,ref_i_3=9,ref_m_1=7,ref_f_2=8,finaldata=_want6);
%calculation(outdataset=tab1,ref_tab1=6,ref_i_3=10,ref_m_1=8,ref_f_2=9,finaldata=_want7);

PROC SQL;
SELECT MEMNAME INTO :DATASETNAME SEPARATED  BY ' ' FROM DICTIONARY.TABLES WHERE LIBNAME='WORK' AND MEMNAME LIKE '_W%';
QUIT;

DATA FINAL;
SET &amp;amp;DATASETNAME;
RUN;

PROC SORT;BY CART;RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks &amp;amp; Regards:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 05 Jun 2019 04:29:08 GMT</pubDate>
    <dc:creator>singhsahab</dc:creator>
    <dc:date>2019-06-05T04:29:08Z</dc:date>
    <item>
      <title>join tables with "lag"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/join-tables-with-quot-lag-quot/m-p/563607#M10909</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please,&amp;nbsp;can anybody help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to join two tables (TABLE 1 and TABLE 20 to get TABLE 3, as I explain and exemplify it in the attached excel.) Can you help me, because I am not able to create this code.&lt;/P&gt;&lt;P&gt;I need to do this in SAS in sql, for a number of variables and a larger amount of values. What I send in excel is just one example.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2019 20:46:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/join-tables-with-quot-lag-quot/m-p/563607#M10909</guid>
      <dc:creator>usuario_estudo</dc:creator>
      <dc:date>2019-06-04T20:46:52Z</dc:date>
    </item>
    <item>
      <title>Re: join tables with "lag"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/join-tables-with-quot-lag-quot/m-p/563638#M10917</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the solution&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input CART $	INI_3	MED_1	FIN_2;
datalines;
A 1.50 2.90 4.30
B 1.30 2.40 4.80
;
run;

data table2;
input REF	INI	MED	FIN;
datalines;
1 1.44 9.50 5.76
2 3.70 7.80 6.80
3 5.96 6.10 7.84
4 8.22 4.40 8.88
5 10.48 2.70 9.92
6 12.74 1.00 10.96
7 4.70 6.98 11.00
8 17.26 12.96 11.04
9 29.82 18.94 11.08
10 42.38 24.92 11.12
;
run;

proc sql;
create table test as
select a.*,b.* from table1 as a , table2 as b;
quit;

options minoperator mindelimiter=',';
%macro calculation(outdataset=,ref_tab1=,ref_i_3=,ref_m_1=,ref_f_2=,finaldata=);
proc sql;
create table &amp;amp;outdataset as
select a.cart,a.ini_3,a.FIN_2,a.MED_1,b.FIN,b.MED,b.ini,b.ref as ref_b,a.ref from
test(where=(ref eq &amp;amp;ref_tab1)) as a , test (where=(ref in (&amp;amp;ref_i_3.,&amp;amp;ref_m_1.,&amp;amp;ref_f_2.))) as b where a.cart=b.cart ;
quit; 

proc sql;
create table &amp;amp;finaldata as
select cart,sum(output1) as output from
(select cart,sum(ind1,med1,fin2) as output1 from (
select cart,
case when ref eq &amp;amp;ref_tab1. and ref_b eq &amp;amp;ref_i_3. then ini_3*ini else . end as ind1,
case when ref eq &amp;amp;ref_tab1. and ref_b eq &amp;amp;ref_m_1. then MED_1*MED else . end as MED1,
case when ref eq &amp;amp;ref_tab1. and ref_b eq &amp;amp;ref_f_2. then FIN_2*FIN else . end as fin2 from &amp;amp;outdataset)group by cart)group by cart;
quit;

%mend;


%calculation(outdataset=tab1,ref_tab1=1,ref_i_3=4,ref_m_1=2,ref_f_2=3,finaldata=_want1);
%calculation(outdataset=tab1,ref_tab1=2,ref_i_3=5,ref_m_1=3,ref_f_2=4,finaldata=_want2);
%calculation(outdataset=tab1,ref_tab1=3,ref_i_3=6,ref_m_1=4,ref_f_2=5,finaldata=_want3);
%calculation(outdataset=tab1,ref_tab1=4,ref_i_3=7,ref_m_1=5,ref_f_2=6,finaldata=_want4);
%calculation(outdataset=tab1,ref_tab1=5,ref_i_3=8,ref_m_1=6,ref_f_2=7,finaldata=_want5);
%calculation(outdataset=tab1,ref_tab1=6,ref_i_3=9,ref_m_1=7,ref_f_2=8,finaldata=_want6);
%calculation(outdataset=tab1,ref_tab1=6,ref_i_3=10,ref_m_1=8,ref_f_2=9,finaldata=_want7);

PROC SQL;
SELECT MEMNAME INTO :DATASETNAME SEPARATED  BY ' ' FROM DICTIONARY.TABLES WHERE LIBNAME='WORK' AND MEMNAME LIKE '_W%';
QUIT;

DATA FINAL;
SET &amp;amp;DATASETNAME;
RUN;

PROC SORT;BY CART;RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks &amp;amp; Regards:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 04:29:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/join-tables-with-quot-lag-quot/m-p/563638#M10917</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2019-06-05T04:29:08Z</dc:date>
    </item>
  </channel>
</rss>

