<?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: Macro Loop for Merge in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810168#M319478</link>
    <description>i have edited it my comment since it would be difficult to explain</description>
    <pubDate>Wed, 27 Apr 2022 14:22:34 GMT</pubDate>
    <dc:creator>Toni2</dc:creator>
    <dc:date>2022-04-27T14:22:34Z</dc:date>
    <item>
      <title>Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810146#M319471</link>
      <description>&lt;P&gt;i edited since i might have caused some confusion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is my full code which i use to create PSI index for a number of variables (var1 var2 var3....)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create the below summary table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="153"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="89"&gt;Var_Name&lt;/TD&gt;
&lt;TD width="64"&gt;PSI&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Var1&lt;/TD&gt;
&lt;TD&gt;value&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Var2&lt;/TD&gt;
&lt;TD&gt;value&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;…&lt;/TD&gt;
&lt;TD&gt;value&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Var100&lt;/TD&gt;
&lt;TD&gt;value&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and i want to be part of the existing macro&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;my understanding is that i need to use this line of the code :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data modeling_oot_&amp;amp;i;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;*** Part I: Define Data Set and Variables ****;

%let inputset= dataset1; /* The modeling sample */
%let compareset= dataset2; /* The validation sample */
%let varnum=var1 var2 var3 var4;/* list of numeric variables */
%let binnum=10; /* number of bins for numeric variables */
%let vartxt=; /* list of character variables */
%let imtxt=____; /* label for missing character values */
%let missnum=-999999999; /* label for missing character values */
%let yaxislabel=% Frequency; /* label for distribution */
%let labelmod=Population; /* label for modeling sample */
%let labeloot=Sample; /* label for validation sample */


********* Part II: Numeric Variables *********;

%macro dealnum;

%if %sysfunc(countw(&amp;amp;varnum dummyfill)) &amp;gt; 0 %then %do;

data check_contents;
retain &amp;amp;varnum;
set &amp;amp;inputset(keep=&amp;amp;varnum obs=1);
run;

proc contents data=check_contents varnum out=check_contents2 noprint;
run;

proc sort data=check_contents2(keep=name varnum) out=checkfreq(rename=(name=tablevar)); 
by varnum; 
run;

data varcnt; 
set checkfreq; 
varcnt+1;
run;

proc sql noprint;
select tablevar into :varmore separated by ' ' from varcnt;
quit;

proc sql;
create table vcnt as select count(*) as vcnt from varcnt;
quit;

data _null_; 
set vcnt;
call symputx('vmcnt', vcnt);
run;

proc sql noprint; 
select tablevar into :v1-:v&amp;amp;vmcnt from varcnt;
quit;

proc rank data=&amp;amp;inputset group=&amp;amp;binnum out=check_rank ties=low;
var &amp;amp;varnum;
ranks rank1-rank&amp;amp;vmcnt; 
run;

data check_rank;
set check_rank;
array fillmiss(*) rank1-rank&amp;amp;vmcnt;
do j=1 to dim(fillmiss);
if fillmiss(j)=. then fillmiss(j)=-1;
fillmiss(j)=fillmiss(j)+1;
end;
drop j;
run;

%macro meannum;
%do i=1 %to &amp;amp;vmcnt;

proc means data=check_rank nway min max median noprint;
class rank&amp;amp;i;
var &amp;amp;&amp;amp;v&amp;amp;i;
output out=check&amp;amp;i(drop=_type_ rename=(_freq_=freq_&amp;amp;i))
min=min_v&amp;amp;i
max=max_v&amp;amp;i
median=&amp;amp;&amp;amp;v&amp;amp;i;
run;

data check&amp;amp;i; 
set check&amp;amp;i; 
rank_num_&amp;amp;i+1; 
run;

proc sql noprint; 
select max(rank_num_&amp;amp;i) into :maxrank from check&amp;amp;i;
quit;

data check&amp;amp;i;
length sas_code $ 256.;
set check&amp;amp;i;
if rank_num_&amp;amp;i=1 then sas_code="if &amp;amp;&amp;amp;v&amp;amp;i le "||max_v&amp;amp;i||" then rank_num_&amp;amp;i=1;";
else sas_code="else if &amp;amp;&amp;amp;v&amp;amp;i le "||max_v&amp;amp;i||" then rank_num_&amp;amp;i="||rank_num_&amp;amp;i||";";
if rank_num_&amp;amp;i=&amp;amp;maxrank then sas_code="else rank_num_&amp;amp;i="||rank_num_&amp;amp;i||";";
sas_code=compbl(sas_code);
run;

proc sort data=check&amp;amp;i;
by rank_num_&amp;amp;i;
run;

proc sql noprint;
select sas_code into :algnum&amp;amp;i separated by ' ' from check&amp;amp;i;
quit;

data check_mod_sample;
set check_rank;
&amp;amp;&amp;amp;algnum&amp;amp;i;
run;

data check_oot_sample; 
set &amp;amp;compareset; 
&amp;amp;&amp;amp;algnum&amp;amp;i;
run;

proc freq data=check_mod_sample noprint;
tables rank_num_&amp;amp;i/out=modeling_freq(rename=(count=count_mod percent=freq_mod));
run;

proc freq data=check_oot_sample noprint;
tables rank_num_&amp;amp;i/out=oot_freq(rename=(count=count_oot percent=freq_oot));
run;

proc sort data=modeling_freq;
by rank_num_&amp;amp;i;
run;

proc sort data=oot_freq;
by rank_num_&amp;amp;i;
run;

proc sort data=check&amp;amp;i;
by rank_num_&amp;amp;i;
run;

proc sql noprint;
select count(*) into :totcntoot from check_oot_sample;
quit;

proc sql noprint;
select sum(count_mod) into :totcntmod from modeling_freq;
quit;

proc sql noprint; 
select sum(count_oot) into :totcntoot from oot_freq;
quit;

proc sql noprint;
select sum(freq_mod) into :totfreqmod from modeling_freq;
quit;

proc sql noprint;
select sum(freq_oot) into :totfreqoot from oot_freq;
quit;

data modeling_oot_freq;
merge modeling_freq oot_freq check&amp;amp;i(keep=rank_num_&amp;amp;i &amp;amp;&amp;amp;v&amp;amp;i sas_code);
by rank_num_&amp;amp;i;
if count_oot=. then count_oot=0;
if freq_oot=. then freq_oot=1/&amp;amp;totcntoot;
freq_mod=freq_mod/100;
freq_oot=freq_oot/100;
if freq_mod &amp;gt; freq_oot then PSI=(freq_oot-freq_mod)*log(freq_oot/freq_mod);
else PSI=(freq_mod-freq_oot)*log(freq_mod/freq_oot);
order_rank=put(rank_num_&amp;amp;i, 32.);
run;

proc sql noprint; select sum(PSI) into :psi from modeling_oot_freq; quit;
data for_total;
order_rank="Total";
PSI=&amp;amp;psi;
count_mod=&amp;amp;totcntmod;
count_oot=&amp;amp;totcntoot;
freq_mod=&amp;amp;totfreqmod/100;
freq_oot=&amp;amp;totfreqoot/100;
run;

data modeling_oot_&amp;amp;i; 
set modeling_oot_freq for_total;
keep order_rank PSI;
where order_rank="Total";
order_rank="&amp;amp;&amp;amp;v&amp;amp;i";
format freq_mod 6.4;
format freq_oot 6.4;
informat freq_mod 6.4;
format freq_oot 6.4;
run;



proc print data=modeling_oot_&amp;amp;i /*(drop=sas_code)*/ noobs; /*title "&amp;amp;&amp;amp;v&amp;amp;i"; */;
run;

data modeling_oot_for_graph;
set modeling_oot_&amp;amp;i;
if compress(order_rank)='1' and &amp;amp;&amp;amp;v&amp;amp;i=. then &amp;amp;&amp;amp;v&amp;amp;i=&amp;amp;missnum;
run;

%end;
%mend meannum;
%meannum;
%end;
%mend dealnum;
%dealnum;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 14:21:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810146#M319471</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-04-27T14:21:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810147#M319472</link>
      <description>&lt;P&gt;No macro needed&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have_1 have_2 have_3 have_4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, this is not a "merge".&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 13:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810147#M319472</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-27T13:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810148#M319473</link>
      <description>&lt;P&gt;Maybe you want this ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro append(dsn=);
proc append base=want1 data=&amp;amp;dsn. force;run;
%mend;

proc delete data=want1;run;
%append(dsn=want2)
%append(dsn=want3)
%append(dsn=want4)
&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Apr 2022 13:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810148#M319473</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-04-27T13:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810168#M319478</link>
      <description>i have edited it my comment since it would be difficult to explain</description>
      <pubDate>Wed, 27 Apr 2022 14:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810168#M319478</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-04-27T14:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810171#M319479</link>
      <description>thanks it works but it causes "warning". I have edited my initial comment in order which will make it easier</description>
      <pubDate>Wed, 27 Apr 2022 14:25:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810171#M319479</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-04-27T14:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810173#M319480</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/372747"&gt;@Toni2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;i have edited it my comment since it would be difficult to explain&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is dramatically different than your original question ... it will take me some time to look through this and come up with an answer.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 14:29:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810173#M319480</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-27T14:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810175#M319481</link>
      <description>yes, i know, i am sorry...thanks for the support</description>
      <pubDate>Wed, 27 Apr 2022 14:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810175#M319481</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-04-27T14:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810179#M319483</link>
      <description>&lt;P&gt;So your edit has moved the post from too little information to too much. Now it is is not clear what part of that long program you are having issues with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that you want to add either a PROC APPEND step (if the datasets have consistent structure) or a DATA step (if they don't) somewhere after you have created&amp;nbsp;modeling_oot_&amp;amp;i dataset and before the %END for the %DO I= loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc append base=model_oot_all data=modeling_oot_&amp;amp;i;
run;

data model_oot_all;
   set 
%if &amp;amp;I &amp;gt; 1 %then model_oot_all ;
       modeling_oot_&amp;amp;i
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS&amp;nbsp; Your program has the macro definitions interwoven with the execution.&amp;nbsp; To save your sanity and make it possible to edit the code move the macro definitions to the top. Do not nest macro definitions inside each other.&amp;nbsp; You can see the confusion that causes by just looking at these lines near the bottom of your code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%end;
%mend meannum;
%meannum;
%end;
%mend dealnum;
%dealnum;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have two macros named %DEALNUM and %MEANNUM then the program flow should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro meannum;
...
%mend meannum;
%macro dealnum;
...
  %meanum;
...
%mend dealnum;
&lt;BR /&gt;* Start of actual executable code ;
* setup SAS code to make input data ;
%dealnum;
*cleanup SAS code ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 14:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810179#M319483</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-27T14:47:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810181#M319485</link>
      <description>&lt;P&gt;Great, it works! your understanding is correct. Just one point, when i run it multiple times it replicates the results - see below&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="245"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="65"&gt;PSI&lt;/TD&gt;
&lt;TD width="180"&gt;order_rank&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.008232&lt;/TD&gt;
&lt;TD&gt;Var1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.003461&lt;/TD&gt;
&lt;TD&gt;Var2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.006708&lt;/TD&gt;
&lt;TD&gt;Var3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.000037&lt;/TD&gt;
&lt;TD&gt;Var4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.008232&lt;/TD&gt;
&lt;TD&gt;Var1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.003461&lt;/TD&gt;
&lt;TD&gt;Var2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.006708&lt;/TD&gt;
&lt;TD&gt;Var3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0.000037&lt;/TD&gt;
&lt;TD&gt;Var4&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 27 Apr 2022 14:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810181#M319485</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-04-27T14:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810186#M319489</link>
      <description>&lt;P&gt;Part of the setup is to make sure the dataset you are using to aggregate into does not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Delete or rename the output dataset before running again. Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=output_all;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why did you run it more than once?&amp;nbsp; Are you debugging the logic?&lt;/P&gt;
&lt;P&gt;Or are both runs important?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Perhaps there is some other macro parameter value that should be use in naming the output dataset?&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Or added as a value in an additional variable in the output dataset so you can distinguish which runs the observations are from.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Wed, 27 Apr 2022 15:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810186#M319489</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-27T15:09:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop for Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810191#M319493</link>
      <description>&lt;P&gt;thanks, yes, it works but it still produces replicates. Possibly, i don't place it in the right location.&lt;BR /&gt;&lt;BR /&gt;I run the code multiple times as i make changes.&lt;BR /&gt;&lt;BR /&gt;I want every time which i run it to get the correct results - if it is possible?&lt;BR /&gt;&lt;BR /&gt;Yes, we can adjust the name of the output&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any idea how we can adjust the code?&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 15:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-for-Merge/m-p/810191#M319493</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-04-27T15:36:36Z</dc:date>
    </item>
  </channel>
</rss>

