<?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 Dates in a loop as a condition for a data pull in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705348#M216354</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create two datasets based on specified dates. The "pull_data" macro does the actually retrieval of data and I just wanted to incorporate it into a new macro where I specify the dataset name and the date constraint for each dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO test();&lt;/P&gt;&lt;P&gt;%let j=1;&lt;BR /&gt;%let vals = PRIOR CURRENT;&lt;BR /&gt;%let dates = "24oct2017" "11aug2020";&lt;BR /&gt;%do %while(%scan(&amp;amp;vals,&amp;amp;j) ne );&lt;BR /&gt;%let i=%scan(&amp;amp;vals, &amp;amp;j);&lt;BR /&gt;%let k=input(%scan(&amp;amp;dates, &amp;amp;j),date9.);&lt;/P&gt;&lt;P&gt;%pull_data()&lt;/P&gt;&lt;P&gt;%let j=%eval(&amp;amp;j+1);&lt;BR /&gt;%end;&lt;BR /&gt;%let j=1;&lt;BR /&gt;%MEND;&lt;/P&gt;&lt;P&gt;%test();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the code errors out and I get the following message.&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable K resolves to input("24oct2017",date9.)&lt;BR /&gt;ERROR: CLI describe error: [IBM][CLI Driver][DB2/AIX64] SQL0104N An unexpected token ")" was found following&lt;BR /&gt;"t("24oct2017",date9.". Expected tokens may include: "&amp;lt;identifier&amp;gt;". SQLSTATE=42601&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code runs fine if I comment out the lines associated with dates.&lt;/P&gt;&lt;P&gt;Please advice on how to fix this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Fri, 11 Dec 2020 18:05:08 GMT</pubDate>
    <dc:creator>jlee8</dc:creator>
    <dc:date>2020-12-11T18:05:08Z</dc:date>
    <item>
      <title>Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705348#M216354</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create two datasets based on specified dates. The "pull_data" macro does the actually retrieval of data and I just wanted to incorporate it into a new macro where I specify the dataset name and the date constraint for each dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO test();&lt;/P&gt;&lt;P&gt;%let j=1;&lt;BR /&gt;%let vals = PRIOR CURRENT;&lt;BR /&gt;%let dates = "24oct2017" "11aug2020";&lt;BR /&gt;%do %while(%scan(&amp;amp;vals,&amp;amp;j) ne );&lt;BR /&gt;%let i=%scan(&amp;amp;vals, &amp;amp;j);&lt;BR /&gt;%let k=input(%scan(&amp;amp;dates, &amp;amp;j),date9.);&lt;/P&gt;&lt;P&gt;%pull_data()&lt;/P&gt;&lt;P&gt;%let j=%eval(&amp;amp;j+1);&lt;BR /&gt;%end;&lt;BR /&gt;%let j=1;&lt;BR /&gt;%MEND;&lt;/P&gt;&lt;P&gt;%test();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the code errors out and I get the following message.&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable K resolves to input("24oct2017",date9.)&lt;BR /&gt;ERROR: CLI describe error: [IBM][CLI Driver][DB2/AIX64] SQL0104N An unexpected token ")" was found following&lt;BR /&gt;"t("24oct2017",date9.". Expected tokens may include: "&amp;lt;identifier&amp;gt;". SQLSTATE=42601&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code runs fine if I comment out the lines associated with dates.&lt;/P&gt;&lt;P&gt;Please advice on how to fix this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 18:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705348#M216354</guid>
      <dc:creator>jlee8</dc:creator>
      <dc:date>2020-12-11T18:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705355#M216358</link>
      <description>&lt;P&gt;First problem is this line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let k=input(%scan(&amp;amp;dates, &amp;amp;j),date9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro processor will evaluate the items with triggers (% and &amp;amp;) so when J=1 it is the same as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let k=input(&lt;SPAN&gt;"24oct2017"&lt;/SPAN&gt;,date9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you could use &amp;amp;k to generate a SAS statement like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  date =  &amp;amp;k;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which would work in a data step since the data step compiler will see:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date = input("24oct2017",date9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So whether that is an issue or not depends on how the macro variable K is used in the macro you called but didn't provide.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since your strings are already close to the valid syntax for date literals you might try just using those.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let k=%scan(&amp;amp;dates, &amp;amp;j)d ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really wanted to store the date value (number of days since 1960) in the macro variable K then you can use %SYSFUNC() to run the input in the macro processor.&amp;nbsp; But then you need to use the INPUTN() function. And you need to lose the quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dates = 24oct2017 11aug2020;
...
%let k=%sysfunc(inputN(%scan(&amp;amp;dates, &amp;amp;j),date9.));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Dec 2020 18:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705355#M216358</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-11T18:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705372#M216369</link>
      <description>&lt;P&gt;Thanks Tom.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do use &amp;amp;k as you mentioned in my proc sql statement like this:&amp;nbsp; WHERE DT = &amp;amp;k; but still get the same error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the other suggestion using the sysfunc but ended up with another error:&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable K resolves to 21116&lt;BR /&gt;ERROR: CLI describe error: [IBM][CLI Driver][DB2/AIX64] SQL0401N The data types of the operands for the operation "=" are not&lt;BR /&gt;compatible or comparable. SQLSTATE=42818&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So still looking for answers.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 19:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705372#M216369</guid>
      <dc:creator>jlee8</dc:creator>
      <dc:date>2020-12-11T19:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705375#M216371</link>
      <description>&lt;P&gt;You left out the fact that you are trying to generate DB2 code and not SAS code.&lt;/P&gt;
&lt;P&gt;So you need to know what is the DB2 equivalent of the SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE DT = "01JAN2020"d &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This requires that you know what type of variable DT is in your database (date, datetime, string, number, etc) and how DB2 expects you to type a constant value for that data type.&lt;/P&gt;
&lt;P&gt;So you might want to generate something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE DT = DATE '2020-01-01'&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Dec 2020 19:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705375#M216371</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-11T19:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705377#M216372</link>
      <description>&lt;P&gt;Strongly suggest that make the things that your "pull_data" macro needs as explicit parameters. Assuming that macro values exist and have the correct values the way you are coding is just waiting to be a serious problem when some logic, like making incorrect/incompatible macro variables, goes awry.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So likely your macro should be defined with something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend pull_data (inputdataset = , outputdataset=, othervar= ).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Comparison with blank for macro variables/ values is a pretty touchy problem. Instead of %do %while you might consider using an iterated %do loop using the number of values.&lt;/P&gt;
&lt;PRE&gt;%macro dummy(varlist = );

   %do i=1 %to %sysfunc(countw(&amp;amp;varlist.));
      %let parm = %scan(&amp;amp;varlist.,&amp;amp;i.);
      %put The parm is: &amp;amp;parm.;
      /* code that uses the parm goes here*/
   %end;
%mend;

%dummy(varlist = this that another);&lt;/PRE&gt;
&lt;P&gt;I suspect you are abusing the input function in the macro variable K but since you have not bothered to show the code that actually uses the variable this is supposition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best practice when debugging macros: set OPTIONS MPRINT; before running the macro.&lt;/P&gt;
&lt;P&gt;When you get the errors you need to show the log for the entire result of calling the macro. Copy from the log and paste into a code box opened on the forum with the &amp;lt;/&amp;gt; icon to&amp;nbsp; preserve formatting of the text.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 19:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705377#M216372</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-11T19:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705384#M216378</link>
      <description>&lt;P&gt;Thanks, I have changed my code as you suggested.&amp;nbsp; I will try to incorporate the other things soon.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;vals.));&lt;/P&gt;&lt;P&gt;%let x=%scan(&amp;amp;vals, &amp;amp;i);&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 20:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705384#M216378</guid>
      <dc:creator>jlee8</dc:creator>
      <dc:date>2020-12-11T20:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705396#M216384</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343772"&gt;@jlee8&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, I have changed my code as you suggested.&amp;nbsp; I will try to incorporate the other things soon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;vals.));&lt;/P&gt;
&lt;P&gt;%let x=%scan(&amp;amp;vals, &amp;amp;i);&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have values that might be interpreted as delimiters for the SCAN or COUNTW you may need to restrict the delimiter used. The following is an example that forces both Scan and Countw to use only the blank character for a delimiter.&lt;/P&gt;
&lt;PRE&gt;%macro dummy(varlist = );

   %do i=1 %to %sysfunc(countw(&amp;amp;varlist.,' '));
      %let parm = %scan(&amp;amp;varlist.,&amp;amp;i.,%str( ));
      %put The parm is: &amp;amp;parm.;
      /* code that uses the parm goes here*/
   %end;
%mend;

%dummy(varlist = this-that another);&lt;/PRE&gt;
&lt;P&gt;Since Countw is a data step function you can provide a space the same as in a data step.&lt;/P&gt;
&lt;P&gt;However the %scan as macro function behaves&amp;nbsp; a bit differently and uses the %str( ) function to indicate what is between the ( ) should be treated as a value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 20:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705396#M216384</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-11T20:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: Dates in a loop as a condition for a data pull</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705401#M216388</link>
      <description>&lt;P&gt;It is a little dangerous to use both single quote and space as the delimiter in the call to COUNTW() but then use only space in the call to %SCAN().&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 21:43:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dates-in-a-loop-as-a-condition-for-a-data-pull/m-p/705401#M216388</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-11T21:43:50Z</dc:date>
    </item>
  </channel>
</rss>

