<?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: Create new Varaibles by number of months since loan month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629062#M185981</link>
    <description>&lt;P&gt;Here you go.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data scores;
  set score19:;
  format month_loan yymmn4.;
  month_loan=input(put(month,z4.),yymmn4.);
run;

proc sql;
  create table inter as
  select 
    l.Sum_Loan as Sum_Loan, 
    s.Month_Loan as Month_Loan, 
    l.Date_Loan as Date_Loan, 
    l.id as ID, 
    s.score 
  from loans l left join scores s
  on l.id=s.id and l.date_loan&amp;lt;s.month_loan
  order by l.id, l.date_loan, s.month_loan
  ;
quit;

data want(drop=_: score);
  array Score_ {11} 8. Score_11 - Score_1;
  retain Score_;
  set inter;
  by id date_Loan;
  _n+1;
  Score_[12-_n]=score;
  if last.date_loan then
    do;
      output;
      call missing(_n, of Score_[*]);
    end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Mar 2020 09:52:11 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-03-03T09:52:11Z</dc:date>
    <item>
      <title>Create new Varaibles by number of months since loan month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629041#M185966</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have the following row data tables:&lt;/P&gt;
&lt;P&gt;1-Loans table that has information about loans that customer took during year 2019.&lt;/P&gt;
&lt;P&gt;2-Information about score in each month for each customer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I merge the tables and get a table that contain information of&amp;nbsp; loans and scores during 2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My wanted table will contain information of scores by number of months passed from month_loan.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For customer ID=1 and loan=17OCT2019 there will be : Score1=7&amp;nbsp; Score2=7&amp;nbsp; and Score3 until Score11 will have null value&lt;/P&gt;
&lt;P&gt;For customer ID=1 and loan=23NOV2019 there will be : Score1=7&amp;nbsp;&amp;nbsp; and Score2 until Score11 will have null value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;For customer ID=4 and loan=19MAY2019 there will be : Score1=7&amp;nbsp; &amp;nbsp;Score2=7&amp;nbsp; Score3=7&amp;nbsp; Score4=7 Score5=8&amp;nbsp; Score6=8&amp;nbsp; Score7=8 and Score9-Score11 will have null value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&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;%include '!RSMEHOME/SASCode/SHARECode/Libname.sas';	

Data Loans;
Retain ID date_Loan Month_Loan Sum_Loan ; 
informat date_Loan date9.;
format date_Loan date9.;
input ID date_Loan Sum_Loan;
Month_Loan=put(date_Loan,yymmn4.);
cards;
1 '17OCT2019'd 1000
2 '03JAN2019'd 900
1 '23NOV2019'd 3000
3 '22JUN2019'd 1500
3 '05MAR2019'd 300
4 '19May2019'd 700
;
run;


Data Score1901;
input ID month Score;
cards;
2 1901 5
3 1901 2
1 1901 8
4 1901 8
;
Run;


Data Score1902;
input ID month Score;
cards;
4 1902 8
1 1902 7
2 1902 5
3 1902 3
;
Run;

Data Score1903;
input ID month Score;
cards;
3 1903 2
1 1903 7
2 1903 4
4 1903 8
;
Run;


Data Score1904;
input ID month Score;
cards;
1 1904 6
2 1904 5
3 1904 2
4 1904 8
;
Run;

Data Score1905;
input ID month Score;
cards;
1 1905 6
2 1905 5
3 1905 3
4 1905 7
;
Run;

Data Score1906;
input ID month Score;
cards;
2 1906 4
1 1906 6
3 1906 5
4 1906 7
;
Run;

Data Score1907;
input ID month Score;
cards;
1 1907 8
2 1907 3
3 1907 5
4 1907 7
;
Run;

Data Score1908;
input ID month Score;
cards;
1 1908 8
2 1908 5
3 1908 5
4 1908 7
;
Run;

Data Score1909;
input ID month Score;
cards;
1 1909 7
2 1909 4
3 1909 6
4 1909 7
;
Run;


Data Score1910;
input ID month Score;
cards;
1 1910 9
2 1910 4
3 1910 6
4 1910 8
;
Run;

Data Score1911;
input ID month Score;
cards;
1 1911 7
2 1911 4
3 1911 6
4 1911 8
;
Run;


Data Score1912;
input ID month Score;
cards;
4 1912 8
1 1912 7
2 1912 2
3 1912 9
;
Run;


%let vector=1901+1902+1903+1904+1905+1906+1907+1908+1909+1910+1911+1912;
%let n=12;/*Number of arguments in &amp;amp;vector.*/
%macro mmacro1;
%DO i=1  %TO  &amp;amp;n.;
%let YYMM=%scan(&amp;amp;vector.,&amp;amp;i.,+);
proc sort data=Score&amp;amp;YYMM.;
By ID;
Run;
%end;
%mend mmacro1; 
%mmacro1; 

proc sort data=Loans;by ID;Run;
%macro mmerge;
%DO i=1  %TO  &amp;amp;n.;
%let YYMM=%scan(&amp;amp;vector.,&amp;amp;i.,+);
Score&amp;amp;YYMM.(keep=Id score rename=(Score=Score&amp;amp;YYMM.))
%end;
%mend mmerge; 
%mmacro1; 


Data tbl1;
Merge Loans %mmerge;
by ID;
Run;


 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="20200303_101142.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36532iE722E3AED9BE566C/image-size/large?v=v2&amp;amp;px=999" role="button" title="20200303_101142.jpg" alt="20200303_101142.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 08:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629041#M185966</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-03-03T08:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create new Varaibles by number of months since loan month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629044#M185968</link>
      <description>&lt;P&gt;Maybe i am to short-sighted, but what's the question?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 08:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629044#M185968</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-03-03T08:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create new Varaibles by number of months since loan month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629045#M185969</link>
      <description>&lt;P&gt;The question is how to create "wanted" data set as you see in the photo?&lt;/P&gt;
&lt;P&gt;I have also explained the logic that I need to have information about scores from 1 month after the month that loan was taken .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 08:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629045#M185969</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-03-03T08:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create new Varaibles by number of months since loan month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629062#M185981</link>
      <description>&lt;P&gt;Here you go.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data scores;
  set score19:;
  format month_loan yymmn4.;
  month_loan=input(put(month,z4.),yymmn4.);
run;

proc sql;
  create table inter as
  select 
    l.Sum_Loan as Sum_Loan, 
    s.Month_Loan as Month_Loan, 
    l.Date_Loan as Date_Loan, 
    l.id as ID, 
    s.score 
  from loans l left join scores s
  on l.id=s.id and l.date_loan&amp;lt;s.month_loan
  order by l.id, l.date_loan, s.month_loan
  ;
quit;

data want(drop=_: score);
  array Score_ {11} 8. Score_11 - Score_1;
  retain Score_;
  set inter;
  by id date_Loan;
  _n+1;
  Score_[12-_n]=score;
  if last.date_loan then
    do;
      output;
      call missing(_n, of Score_[*]);
    end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Mar 2020 09:52:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-Varaibles-by-number-of-months-since-loan-month/m-p/629062#M185981</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-03T09:52:11Z</dc:date>
    </item>
  </channel>
</rss>

