<?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 detect outliers using  macro function in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724302#M35110</link>
    <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;Any help will be appreciated, below is the macro function sas code to&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;detect outliers :&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;I do not understand these sections: "&lt;CODE class=" language-sas"&gt; Q1 = &amp;amp;Q1 &amp;amp;val._P25;","&lt;CODE class=" language-sas"&gt; varL = &amp;amp;varL &amp;amp;val.L;&lt;/CODE&gt;",&amp;nbsp;&lt;/CODE&gt;&lt;SPAN style="font-family: inherit;"&gt;what sections in sas documentation explain theses parts&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;?&lt;/CODE&gt;&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;How can i detect outliers using this macro function?&lt;/LI&gt;&lt;LI&gt;How can i change the lower and upper limit to 10percentile and 90percentile?&lt;/LI&gt;&lt;LI&gt;How can i get only outliers that are larger than 90percentile?&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro outliers(input=, var=, output= );

%let Q1=;
%let Q3=;
%let varL=;
%let varH=;

%let n=%sysfunc(countw(&amp;amp;var));
%do i= 1 %to &amp;amp;n;
%let val = %scan(&amp;amp;var,&amp;amp;i);
%let &lt;FONT color="#000000"&gt;Q1 = &amp;amp;Q1 &amp;amp;val._P25&lt;/FONT&gt;;
%let Q3 = &amp;amp;Q3 &amp;amp;val._P75;
%let varL = &amp;amp;varL &amp;amp;val.L;
%let varH = &amp;amp;varH &amp;amp;val.H;
%end;

/* Calculate the quartiles and inter-quartile range using proc univariate */
proc means data=&amp;amp;input nway noprint;
var &amp;amp;var;
output out=temp P25= P75= / autoname;
run;

/* Extract the upper and lower limits into macro variables */
data temp;
set temp;
ID = 1;
array varb(&amp;amp;n) &amp;amp;Q1;
array varc(&amp;amp;n) &amp;amp;Q3;
array lower(&amp;amp;n) &amp;amp;varL;
array upper(&amp;amp;n) &amp;amp;varH;
do i = 1 to dim(varb);
lower(i) = varb(i) - 3 * (varc(i) - varb(i));
upper(i) = varc(i) + 3 * (varc(i) - varb(i));
end;
drop i _type_ _freq_;
run;

data temp1;
set &amp;amp;input;
ID = 1;
run;
data &amp;amp;output;
merge temp1 temp;
by ID;
array var(&amp;amp;n) &amp;amp;var;
array lower(&amp;amp;n) &amp;amp;varL;
array upper(&amp;amp;n) &amp;amp;varH;
do i = 1 to dim(var);
if not missing(var(i)) then do;
if var(i) &amp;gt;= lower(i) and var(i) &amp;lt;= upper(i);
end;
end;
drop &amp;amp;Q1 &amp;amp;Q3 &amp;amp;varL &amp;amp;varH ID i;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 07 Mar 2021 20:23:18 GMT</pubDate>
    <dc:creator>fatemeh</dc:creator>
    <dc:date>2021-03-07T20:23:18Z</dc:date>
    <item>
      <title>detect outliers using  macro function</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724302#M35110</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;Any help will be appreciated, below is the macro function sas code to&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;detect outliers :&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;I do not understand these sections: "&lt;CODE class=" language-sas"&gt; Q1 = &amp;amp;Q1 &amp;amp;val._P25;","&lt;CODE class=" language-sas"&gt; varL = &amp;amp;varL &amp;amp;val.L;&lt;/CODE&gt;",&amp;nbsp;&lt;/CODE&gt;&lt;SPAN style="font-family: inherit;"&gt;what sections in sas documentation explain theses parts&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;?&lt;/CODE&gt;&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;How can i detect outliers using this macro function?&lt;/LI&gt;&lt;LI&gt;How can i change the lower and upper limit to 10percentile and 90percentile?&lt;/LI&gt;&lt;LI&gt;How can i get only outliers that are larger than 90percentile?&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro outliers(input=, var=, output= );

%let Q1=;
%let Q3=;
%let varL=;
%let varH=;

%let n=%sysfunc(countw(&amp;amp;var));
%do i= 1 %to &amp;amp;n;
%let val = %scan(&amp;amp;var,&amp;amp;i);
%let &lt;FONT color="#000000"&gt;Q1 = &amp;amp;Q1 &amp;amp;val._P25&lt;/FONT&gt;;
%let Q3 = &amp;amp;Q3 &amp;amp;val._P75;
%let varL = &amp;amp;varL &amp;amp;val.L;
%let varH = &amp;amp;varH &amp;amp;val.H;
%end;

/* Calculate the quartiles and inter-quartile range using proc univariate */
proc means data=&amp;amp;input nway noprint;
var &amp;amp;var;
output out=temp P25= P75= / autoname;
run;

/* Extract the upper and lower limits into macro variables */
data temp;
set temp;
ID = 1;
array varb(&amp;amp;n) &amp;amp;Q1;
array varc(&amp;amp;n) &amp;amp;Q3;
array lower(&amp;amp;n) &amp;amp;varL;
array upper(&amp;amp;n) &amp;amp;varH;
do i = 1 to dim(varb);
lower(i) = varb(i) - 3 * (varc(i) - varb(i));
upper(i) = varc(i) + 3 * (varc(i) - varb(i));
end;
drop i _type_ _freq_;
run;

data temp1;
set &amp;amp;input;
ID = 1;
run;
data &amp;amp;output;
merge temp1 temp;
by ID;
array var(&amp;amp;n) &amp;amp;var;
array lower(&amp;amp;n) &amp;amp;varL;
array upper(&amp;amp;n) &amp;amp;varH;
do i = 1 to dim(var);
if not missing(var(i)) then do;
if var(i) &amp;gt;= lower(i) and var(i) &amp;lt;= upper(i);
end;
end;
drop &amp;amp;Q1 &amp;amp;Q3 &amp;amp;varL &amp;amp;varH ID i;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Mar 2021 20:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724302#M35110</guid>
      <dc:creator>fatemeh</dc:creator>
      <dc:date>2021-03-07T20:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: detect outliers using  macro function</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724346#M35113</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60061"&gt;@fatemeh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;Any help will be appreciated, below is the macro function sas code to&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;detect outliers :&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;I do not understand these sections: "&lt;CODE class=" language-sas"&gt; Q1 = &amp;amp;Q1 &amp;amp;val._P25;","&lt;CODE class=" language-sas"&gt; varL = &amp;amp;varL &amp;amp;val.L;&lt;/CODE&gt;",&amp;nbsp;&lt;/CODE&gt;&lt;SPAN style="font-family: inherit;"&gt;what sections in sas documentation explain theses parts&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;?&lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These are macro resolution and are literal text replacements, documented here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0700fspmubii5n1vecutttwz93n.htm&amp;amp;locale=en#!"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0700fspmubii5n1vecutttwz93n.htm&amp;amp;locale=en#!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're not familiar with macros and macro variables, that should be your starting point.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60061"&gt;@fatemeh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&amp;nbsp;How can i detect outliers using this macro function?&lt;/LI&gt;
&lt;/OL&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This code removes outliers, it doesn't detect them per se. This line is known as a SUBSETTING IF and will only keep values within the interval. If you want to detect outliers, you could consider reversing the condition to only keep outliers or to flag those records someway instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if var(i) &amp;gt;= lower(i) and var(i) &amp;lt;= upper(i);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The upper and lower bounds are calculated using this formula, not one I'm familiar with.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lower(i) = varb(i) - 3 * (varc(i) - varb(i));
upper(i) = varc(i) + 3 * (varc(i) - varb(i));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60061"&gt;@fatemeh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;How can i change the lower and upper limit to 10percentile and 90percentile?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can change the statistics calculated in the PROC MEANS statement and then need to change all the references going down the code to do that instead of using the rule in this code. If that were the case I would instead suggest you get your own NON MACRO code working and then work on converting your working code to a macro instead. This isn't a particularly efficient macro anyways.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - PROC RANK will easily group data into deciles and anything in the first and last groups would be your outliers for example.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=sashelp.cars out=cars_ranked groups =10;
var mpg_city;
rank rank_mpg_city;
run;

*first and last groups would be outliers;
proc freq data=cars_ranked;
table rank_mpg_city;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60061"&gt;@fatemeh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;How can i get only outliers that are larger than 90percentile&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I suggest using PROC RANK instead, it's much easier in your use case, this would isolate all the observations in the top decile.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars_ranked_outliers;
set cars_ranked;
where rank_mpg_city = 9;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have your new code working here's a tutorial on converting a working program to a macro&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 03:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724346#M35113</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-08T03:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: detect outliers using  macro function</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724358#M35115</link>
      <description>&lt;P&gt;If you have specific question about this Outlier macro please check with the author,&amp;nbsp;&lt;A href="https://www.blogger.com/profile/04301769767832902721" rel="nofollow" target="_blank"&gt;Deepanshu Bhalla&lt;/A&gt; at&amp;nbsp;&lt;A href="https://www.listendata.com/2014/10/identify-and-remove-outliers-with-sas.html" target="_blank"&gt;https://www.listendata.com/2014/10/identify-and-remove-outliers-with-sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;and clarify your specific doubts.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 05:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/detect-outliers-using-macro-function/m-p/724358#M35115</guid>
      <dc:creator>gcjfernandez</dc:creator>
      <dc:date>2021-03-08T05:02:12Z</dc:date>
    </item>
  </channel>
</rss>

