<?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: Loop in Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-Macro/m-p/229327#M41500</link>
    <description>&lt;P&gt;I can outline an approach, but I don't have the time to give you all the details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this variation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc univariate data=&amp;amp;input noprint;&lt;/P&gt;
&lt;P&gt;var &amp;amp;vars;&lt;/P&gt;
&lt;P&gt;output out=ranges (keep=&amp;amp;vars) qrange=;&lt;/P&gt;
&lt;P&gt;output out=q1 (keep=&amp;amp;vars) q1=;&lt;/P&gt;
&lt;P&gt;output out=q3 (keep=&amp;amp;vars) q3=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That gives you three small output data sets (one observation apiece). &amp;nbsp;You can investigate for yourself, but in the Q1 data set, each variable will be the Q1 value for that same original variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next step: &amp;nbsp;transpose the three data sets so you have two columns in each (for example, original variable name, and the Q1 value). &amp;nbsp;You're working with small data sets so the processing time will be minimal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With all three data sets transposed, use a DATA step to read them in and write out IF/THEN statements to a file. &amp;nbsp;Again, you're working with tiny data sets and the processing time will be minimal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, %include the IF/THEN statements in a DATA step to perform the calculations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is conceivable that ODS can save some of the work by producing an output data set with one row per variable and three statistics. &amp;nbsp;I'm not familiar enough with the possible ODS outputs from univariate to know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
    <pubDate>Fri, 09 Oct 2015 16:14:12 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2015-10-09T16:14:12Z</dc:date>
    <item>
      <title>Loop in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-Macro/m-p/194000#M36471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi SAS Experts -&lt;/P&gt;&lt;P&gt;I have a macro that calculates acceptable range of a variable. An acceptable range is defined by :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lower Limit = Q1 - 1.5*(Q3-Q1)&lt;/P&gt;&lt;P&gt;Upper Limit = Q3 + 1.5*(Q3-Q1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's a boxplot method of calculating outliers. The macro is working fine. But it is inefficient in terms of its processing as it calculates outliers for each variable in a loop and then capping values. I want proc univariate to be run for all the variables (not in loop) and save output in a dataset and then capping for variables using IF THEN at one time only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code : -&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;options mprint symbolgen; &lt;/P&gt;&lt;P&gt;%macro outliers(input=, vars=, output= );&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data &amp;amp;output;&lt;/P&gt;&lt;P&gt;set &amp;amp;input;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let n=%sysfunc(countw(&amp;amp;vars));&lt;/P&gt;&lt;P&gt;%do i= 1 %to &amp;amp;n;&lt;/P&gt;&lt;P&gt;%let val = %scan(&amp;amp;vars,&amp;amp;i);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Calculate the quartiles and inter-quartile range using proc univariate */&lt;/P&gt;&lt;P&gt;proc univariate data=&amp;amp;output noprint;&lt;/P&gt;&lt;P&gt;var &amp;amp;val;&lt;/P&gt;&lt;P&gt;output out=temp QRANGE= IQR Q1= First_Qtl Q3= Third_Qtl;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Extract the upper and lower limits into macro variables */&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;set temp;&lt;/P&gt;&lt;P&gt;call symput('QR', IQR);&lt;/P&gt;&lt;P&gt;call symput('Q1', First_Qtl);&lt;/P&gt;&lt;P&gt;call symput('Q3', Third_Qtl);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let ULimit=%sysevalf(&amp;amp;Q3 + 1.5 * &amp;amp;QR);&lt;/P&gt;&lt;P&gt;%let LLimit=%sysevalf(&amp;amp;Q1 - 1.5 * &amp;amp;QR);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Final dataset excluding outliers*/&lt;/P&gt;&lt;P&gt;data &amp;amp;output;&lt;/P&gt;&lt;P&gt;set &amp;amp;output;&lt;/P&gt;&lt;P&gt;if &amp;amp;val &amp;lt; &amp;amp;Llimit then &amp;amp;val = &amp;amp;Llimit;&lt;/P&gt;&lt;P&gt;if &amp;amp;val &amp;gt; &amp;amp;Ulimit then &amp;amp;val = &amp;amp;Ulimit;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%outliers(Input=abcd, Vars = a, output= test);&lt;/P&gt;&lt;P&gt;o &lt;/P&gt;&lt;P&gt;Thanks in anticipation!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Apr 2015 09:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-Macro/m-p/194000#M36471</guid>
      <dc:creator>Riya88</dc:creator>
      <dc:date>2015-04-12T09:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-Macro/m-p/229327#M41500</link>
      <description>&lt;P&gt;I can outline an approach, but I don't have the time to give you all the details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this variation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc univariate data=&amp;amp;input noprint;&lt;/P&gt;
&lt;P&gt;var &amp;amp;vars;&lt;/P&gt;
&lt;P&gt;output out=ranges (keep=&amp;amp;vars) qrange=;&lt;/P&gt;
&lt;P&gt;output out=q1 (keep=&amp;amp;vars) q1=;&lt;/P&gt;
&lt;P&gt;output out=q3 (keep=&amp;amp;vars) q3=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That gives you three small output data sets (one observation apiece). &amp;nbsp;You can investigate for yourself, but in the Q1 data set, each variable will be the Q1 value for that same original variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next step: &amp;nbsp;transpose the three data sets so you have two columns in each (for example, original variable name, and the Q1 value). &amp;nbsp;You're working with small data sets so the processing time will be minimal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With all three data sets transposed, use a DATA step to read them in and write out IF/THEN statements to a file. &amp;nbsp;Again, you're working with tiny data sets and the processing time will be minimal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, %include the IF/THEN statements in a DATA step to perform the calculations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is conceivable that ODS can save some of the work by producing an output data set with one row per variable and three statistics. &amp;nbsp;I'm not familiar enough with the possible ODS outputs from univariate to know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2015 16:14:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-Macro/m-p/229327#M41500</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-10-09T16:14:12Z</dc:date>
    </item>
  </channel>
</rss>

