<?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: using %do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36622#M7214</link>
    <description>You need to pony up the important info which is the exact error you get, preferably as a SASLOG excerpt, pasted in your post-reply, so others can help you.  All this presumes that you have turned on the max amount of SAS session diagnostics with your OPTIONS statement,such as:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN;&lt;BR /&gt;
&lt;BR /&gt;
These contributions and self-diagnosis will help greatly, presuming you haven't already solved your problem.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Wed, 06 Aug 2008 19:43:11 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2008-08-06T19:43:11Z</dc:date>
    <item>
      <title>using %do loop - solved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36619#M7211</link>
      <description>Hi. &lt;BR /&gt;
I am new to SAS but not new to programming (which might be a hinderance in learning how SAS works).  I am dealing with a huge amount of data that has to be extracted from SQL Server using ODBC in chunks, or my connection times out.&lt;BR /&gt;
&lt;BR /&gt;
I am trying to run an sql statement 4 times with different dates each time.  I want to substitute the from and to date in the sql statement with calculated dates.&lt;BR /&gt;
&lt;BR /&gt;
The first iteration works just fine, but the subsequent iterations give an error when recalculating the dates.&lt;BR /&gt;
&lt;BR /&gt;
Here is the code:&lt;BR /&gt;
ods output SQL_Results(persist=run) = Mcd.ProfSummary_OtherPayments;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
%Do mm = 12 %to 3 %by -3; /* break sql into 4 quarters due to time to run */&lt;BR /&gt;
StartDate_sas = intnx('month',today(), (-1 * &amp;amp;mm), 'Beginning');&lt;BR /&gt;
EndDate_sas = intnx('month',today(),(-1 * (&amp;amp;mm - 3)),'Beginning'); /* 1st day of  next qtr */&lt;BR /&gt;
&lt;BR /&gt;
StartDate_mdy = "'" || put(StartDate_sas,mmddyyd10.) || "'"; /* add apostrophes */&lt;BR /&gt;
EndDate_mdy = "'" || put(EndDate_sas,mmddyyd10.) || "'";     /* for sql statement */&lt;BR /&gt;
call symput('StartDate', StartDate_mdy); &lt;BR /&gt;
call symput('EndDate', EndDate_mdy); &lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
 connect to odbc as user1&lt;BR /&gt;
   (datasrc="IDX Analyzer" user=&amp;amp;User_Id. password=&amp;amp;Pass_word.);&lt;BR /&gt;
&lt;BR /&gt;
select * from connection to User1&lt;BR /&gt;
/* run in 4 3 month increments - too many records */&lt;BR /&gt;
&lt;BR /&gt;
(Select lic.invoice_number,LIC.MEDICAL_RECORD_NUMBER, &lt;BR /&gt;
        LIC.Total_Invoice_Charges, &lt;BR /&gt;
        LIC.ADMISSION_DATE,&lt;BR /&gt;
        sum(LIP.Payment_Amount) as OthAmtPaid, &lt;BR /&gt;
        sum(LIP.Adjustment_Amount) as OthAdjAmt&lt;BR /&gt;
&lt;BR /&gt;
from dbo.view_Prof_LineItem_Charges_Payadj LIC&lt;BR /&gt;
join dbo.invoice_line_item_payment LIP on LIC.Invoice_Number = LIP.Invoice_Number&lt;BR /&gt;
join  dbo.pay_codes pc on LIP.id_2 = pc.id_2 and pc.payment_code_name not like '%medicaid%'&lt;BR /&gt;
&lt;BR /&gt;
where LIC.Admission_date &amp;gt;= &amp;amp;StartDate.&lt;BR /&gt;
  and LIC.Admission_date &amp;lt; &amp;amp;EndDate.&lt;BR /&gt;
  and exists&lt;BR /&gt;
   (select lic2.Invoice_Number &lt;BR /&gt;
    from dbo.view_Prof_LineItem_Charges_Payadj Lic2&lt;BR /&gt;
    join dbo.invoice_line_item_payment LIP2 on LIC2.Invoice_Number = LIP2.Invoice_Number&lt;BR /&gt;
    join  dbo.pay_codes pc2 on LIP2.id_2 = pc2.id_2 and pc2.payment_code_name like '%medicaid%'&lt;BR /&gt;
	where lic2.Invoice_Number = lic.Invoice_Number)&lt;BR /&gt;
&lt;BR /&gt;
group by lic.invoice_number,&lt;BR /&gt;
lic.Medical_record_number, LIC.Total_Invoice_Charges, lic.admission_date)&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%end;  /* do for next quarter */&lt;BR /&gt;
&lt;BR /&gt;
Like I said, the first iteration runs perfectly.  But each subsequent iteration gives me the following error:&lt;BR /&gt;
MLOGIC(GETDATA):  %DO loop index variable MM is now 9; loop will  iterate again.&lt;BR /&gt;
SYMBOLGEN:  Macro variable MM resolves to 9&lt;BR /&gt;
MPRINT(GETDATA):   StartDate_sas = intnx('month',today(), (-1 * 9), 'Beginning');&lt;BR /&gt;
NOTE: Line generated by the invoked macro "GETDATA".&lt;BR /&gt;
12     StartDate_sas = intnx('month',today(), (-1 * &amp;amp;mm), 'Beginning');&lt;BR /&gt;
       -------------&lt;BR /&gt;
       180&lt;BR /&gt;
&lt;BR /&gt;
ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;BR /&gt;
&lt;BR /&gt;
I get the same error for each statement up to the proc sql statement.&lt;BR /&gt;
&lt;BR /&gt;
Any other suggestions on how to accomplish this would be appreciated.&lt;BR /&gt;
&lt;BR /&gt;
Thanks.

Message was edited by: pk</description>
      <pubDate>Wed, 06 Aug 2008 13:56:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36619#M7211</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-06T13:56:30Z</dc:date>
    </item>
    <item>
      <title>Re: using %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36620#M7212</link>
      <description>Hi:&lt;BR /&gt;
  The %DO loop will not work in "open" code. You should be getting these error messages in the SAS log:&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
ERROR: The %DO statement is not valid in open code.&lt;BR /&gt;
&lt;BR /&gt;
ERROR: The %END statement is not valid in open code.&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
You need to read in the SAS documentation about Macro PROGRAMS. Or, consult this SUGI paper for a good introduction to Macro processing:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Wed, 06 Aug 2008 14:47:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36620#M7212</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-08-06T14:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: using %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36621#M7213</link>
      <description>The whole thing is a macro.  I did not include the first lines of code in my original post:&lt;BR /&gt;
%macro GetData;&lt;BR /&gt;
libname Mcd 'S:\Data Requests\Medicaid Analysis';&lt;BR /&gt;
Libname Includes 'S:\Data Requests\SAS Includes'; /* to get id/pwd for connect string */&lt;BR /&gt;
&lt;BR /&gt;
ods listing close;&lt;BR /&gt;
ods HTML close;&lt;BR /&gt;
&lt;BR /&gt;
proc datasets library = Mcd; /* delete old data */&lt;BR /&gt;
  delete ProfSummary_OtherPayments;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* get id and password for ODBC connection from encrypted file */&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set Includes.ODBCConnectStrings (read=DUHSC_21);&lt;BR /&gt;
   where trim(DSN) = 'IDX Analyzer';&lt;BR /&gt;
call symput('User_Id', trim(UID));&lt;BR /&gt;
call symput('Pass_word',trim(PWD));&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
ods output SQL_Results(persist=run) = Mcd.ProfSummary_OtherPayments;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
%Do mm = 12 %to 3 %by -3; /* break sql into 4 quarters due to time to run */&lt;BR /&gt;
StartDate_sas = intnx('month',today(), (-1 * &amp;amp;mm), 'Beginning');&lt;BR /&gt;
EndDate_sas = intnx('month',today(),(-1 * (&amp;amp;mm - 3)),'Beginning'); /* 1st day of next qtr */&lt;BR /&gt;
&lt;BR /&gt;
StartDate_mdy = "'" || put(StartDate_sas,mmddyyd10.) || "'"; /* add apostrophes */&lt;BR /&gt;
EndDate_mdy = "'" || put(EndDate_sas,mmddyyd10.) || "'";     /* for sql statement */&lt;BR /&gt;
call symput('StartDate', StartDate_mdy); &lt;BR /&gt;
call symput('EndDate', EndDate_mdy); &lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
 connect to odbc as user1&lt;BR /&gt;
   (datasrc="IDX Analyzer" user=&amp;amp;User_Id. password=&amp;amp;Pass_word.);&lt;BR /&gt;
&lt;BR /&gt;
select * from connection to User1&lt;BR /&gt;
/* Select 1 year of non-Medicaid data from professional data */&lt;BR /&gt;
/* but only if has Medicaid data too */&lt;BR /&gt;
/* This will be joined with the Medicaid data */&lt;BR /&gt;
/* run in 4 3 month increments - too many records */&lt;BR /&gt;
&lt;BR /&gt;
(Select lic.invoice_number,LIC.MEDICAL_RECORD_NUMBER, &lt;BR /&gt;
        LIC.Total_Invoice_Charges, &lt;BR /&gt;
        LIC.ADMISSION_DATE,&lt;BR /&gt;
        sum(LIP.Payment_Amount) as OthAmtPaid, &lt;BR /&gt;
        sum(LIP.Adjustment_Amount) as OthAdjAmt&lt;BR /&gt;
&lt;BR /&gt;
from dbo.view_Prof_LineItem_Charges_Payadj LIC&lt;BR /&gt;
join dbo.invoice_line_item_payment LIP on LIC.Invoice_Number = LIP.Invoice_Number&lt;BR /&gt;
join  dbo.pay_codes pc on LIP.id_2 = pc.id_2 and pc.payment_code_name not like '%medicaid%'&lt;BR /&gt;
&lt;BR /&gt;
where LIC.Admission_date &amp;gt;= &amp;amp;StartDate.&lt;BR /&gt;
  and LIC.Admission_date &amp;lt; &amp;amp;EndDate.&lt;BR /&gt;
  and exists&lt;BR /&gt;
   (select lic2.Invoice_Number &lt;BR /&gt;
    from dbo.view_Prof_LineItem_Charges_Payadj Lic2&lt;BR /&gt;
    join dbo.invoice_line_item_payment LIP2 on LIC2.Invoice_Number = LIP2.Invoice_Number&lt;BR /&gt;
    join  dbo.pay_codes pc2 on LIP2.id_2 = pc2.id_2 and pc2.payment_code_name like '%medicaid%'&lt;BR /&gt;
	where lic2.Invoice_Number = lic.Invoice_Number)&lt;BR /&gt;
&lt;BR /&gt;
group by lic.invoice_number,&lt;BR /&gt;
lic.Medical_record_number, LIC.Total_Invoice_Charges, lic.admission_date)&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%end;  /* do for next quarter */&lt;BR /&gt;
&lt;BR /&gt;
ods output close;&lt;BR /&gt;
ods html;&lt;BR /&gt;
ods listing; &lt;BR /&gt;
&lt;BR /&gt;
%mend GetData;&lt;BR /&gt;
 %getdata;</description>
      <pubDate>Wed, 06 Aug 2008 14:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36621#M7213</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-06T14:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: using %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36622#M7214</link>
      <description>You need to pony up the important info which is the exact error you get, preferably as a SASLOG excerpt, pasted in your post-reply, so others can help you.  All this presumes that you have turned on the max amount of SAS session diagnostics with your OPTIONS statement,such as:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN;&lt;BR /&gt;
&lt;BR /&gt;
These contributions and self-diagnosis will help greatly, presuming you haven't already solved your problem.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 06 Aug 2008 19:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36622#M7214</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-08-06T19:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: using %do loop - solved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36623#M7215</link>
      <description>Ok, I found the problem - rookie mistake.&lt;BR /&gt;
&lt;BR /&gt;
I had the data _null_; statement outside the %do loop:&lt;BR /&gt;
 data _null_;&lt;BR /&gt;
%Do mm = 12 %to 3 %by -3; /* break sql into 4 quarters due to time to run */&lt;BR /&gt;
     - date manipulation statements -&lt;BR /&gt;
proc sql;&lt;BR /&gt;
     - sql statements -&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
Now, like this, it works as intended:&lt;BR /&gt;
%Do mm = 12 %to 3 %by -3; /* break sql into 4 quarters due to time to run */&lt;BR /&gt;
data _null_;&lt;BR /&gt;
      - date manipulation statments -&lt;BR /&gt;
proc sql;&lt;BR /&gt;
     - sql statments -&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
Thanks for the help.</description>
      <pubDate>Thu, 07 Aug 2008 13:11:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-do-loop-solved/m-p/36623#M7215</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-07T13:11:45Z</dc:date>
    </item>
  </channel>
</rss>

