<?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: PROC SQL INTO issue in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377430#M90626</link>
    <description>&lt;P&gt;Thanks for both of you for the reply,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After this, and still inside the loop, I call another macro with inputfile in parameter.&lt;/P&gt;&lt;P&gt;I prefer to have only a single macrovariable than 37 (inputfile1 to inputfile37), it is easier to manage.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know I could do with a single inputfile?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jul 2017 16:07:11 GMT</pubDate>
    <dc:creator>FP12</dc:creator>
    <dc:date>2017-07-19T16:07:11Z</dc:date>
    <item>
      <title>PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377405#M90615</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro ext_1;
	%let iter = 1;

	%do %while (&amp;amp;iter. &amp;lt;= &amp;amp;nbfile.);
		PROC SQL ; 
			SELECT filename INTO :inputfile  FROM INP_1 WHERE ROWN = &amp;amp;iter.; 
		QUIT;
		%put &amp;amp;inputfile.;
		%let iter = %eval(&amp;amp;iter.+1);
	%end;
%mend;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;nbfile = 37&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My table INP_1 is like this&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10409i3E62DEFE91AF0D49/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture2.PNG" title="Capture2.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run the macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%ext_1;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get a visual resulkt which is ok:&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10410i88796B3331DA18F4/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I am looking at the log (see below an extract for iter from 4 to 6):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;MLOGIC(EXT_1):  %LET (variable name is ITER)
MLOGIC(EXT_1):  %DO %WHILE(&amp;amp;iter. &amp;lt;= &amp;amp;nbfile.) condition is TRUE; loop will iterate again.
MPRINT(EXT_1):   PROC SQL ;
MPRINT(EXT_1):   SELECT filename INTO :inputfile FROM INP_1 WHERE ROWN = 4;
MPRINT(EXT_1):   QUIT;
MLOGIC(EXT_1):  %PUT &amp;amp;inputfile.
P01ADIBP_19906_201610042343_A00000AW.txt
MLOGIC(EXT_1):  %LET (variable name is ITER)
MLOGIC(EXT_1):  %DO %WHILE(&amp;amp;iter. &amp;lt;= &amp;amp;nbfile.) condition is TRUE; loop will iterate again.
MPRINT(EXT_1):   PROC SQL ;
MPRINT(EXT_1):   SELECT filename INTO :inputfile FROM INP_1 WHERE ROWN = 5;
MPRINT(EXT_1):   QUIT;
MLOGIC(EXT_1):  %PUT &amp;amp;inputfile.
P01ADIBP_19906_201610042343_A00000AW.txt
MLOGIC(EXT_1):  %LET (variable name is ITER)
MLOGIC(EXT_1):  %DO %WHILE(&amp;amp;iter. &amp;lt;= &amp;amp;nbfile.) condition is TRUE; loop will iterate again.
MPRINT(EXT_1):   PROC SQL ;
MPRINT(EXT_1):   SELECT filename INTO :inputfile FROM INP_1 WHERE ROWN = 6;
MPRINT(EXT_1):   QUIT;
MLOGIC(EXT_1):  %PUT &amp;amp;inputfile.
P01ADIBP_19906_201610042343_A00000AW.txt&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;inputfile is always&lt;/P&gt;&lt;PRE&gt;P01ADIBP_19906_201610042343_A00000AW.txt&lt;/PRE&gt;&lt;P&gt;which is the last value of my table INP_1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know why?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 15:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377405#M90615</guid>
      <dc:creator>FP12</dc:creator>
      <dc:date>2017-07-19T15:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377422#M90623</link>
      <description>&lt;P&gt;your row number logic is helping you to insert acurately but as you have same name as macrovariable, it is overwritten&amp;nbsp;and Put is picking the last value. To find each value better option is to use do&amp;nbsp; i= 1 %to &amp;amp;count and use &amp;amp;&amp;amp;nputfile&amp;amp;i.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:00:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377422#M90623</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-07-19T16:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377426#M90625</link>
      <description>&lt;P&gt;Yes! It is because you keep replacing its value. What do you want that macro variable to look like after running your macro? Or do you want multiple macro variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In short, what are you trying to achieve?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377426#M90625</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-19T16:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377430#M90626</link>
      <description>&lt;P&gt;Thanks for both of you for the reply,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After this, and still inside the loop, I call another macro with inputfile in parameter.&lt;/P&gt;&lt;P&gt;I prefer to have only a single macrovariable than 37 (inputfile1 to inputfile37), it is easier to manage.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know I could do with a single inputfile?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377430#M90626</guid>
      <dc:creator>FP12</dc:creator>
      <dc:date>2017-07-19T16:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377438#M90629</link>
      <description>&lt;P&gt;If your other code is within the do while loop you showed, it will get the correct values of your macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377438#M90629</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-19T16:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377439#M90630</link>
      <description>&lt;P&gt;Unfortunately not, &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; that is what I try to explain.&lt;/P&gt;&lt;P&gt;The "%put inputfile" is inside the macro loop (see my first message), and it is always equals to the the last table value...&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377439#M90630</guid>
      <dc:creator>FP12</dc:creator>
      <dc:date>2017-07-19T16:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377445#M90634</link>
      <description>&lt;P&gt;Your log should be showing you the correct results as in the following example which I think is doing the same thing as you are doing:&lt;/P&gt;
&lt;PRE&gt;data INP_1;
  informat filename $50.;
  input filename rown;
  cards;
PO1ADIBP_10188_201610050135_A00000AY.txt 1
PO1ADIBP_10188_201610050230_A00000C3.txt 2
PO1ADIBP_10188_2016100502225_A00000C2.txt 3
;

%let nbfile=3;
%macro ext_1;
	%let iter = 1;

	%do %while (&amp;amp;iter. &amp;lt;= &amp;amp;nbfile.);
		PROC SQL ; 
			SELECT filename INTO :inputfile  FROM INP_1 WHERE ROWN = &amp;amp;iter.; 
		QUIT;
		%put &amp;amp;inputfile.;
		%let iter = %eval(&amp;amp;iter.+1);
	%end;
%mend;
%ext_1;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only thing I can think of is that you have both local and global macro variables assigned to the same name. Start a new SAS session and re-run the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377445#M90634</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-19T16:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377447#M90636</link>
      <description>&lt;P&gt;I have made a test simulation of the code and it should work fine.&lt;/P&gt;
&lt;P&gt;Please post your full code including the inner code/macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code I run:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input num word $;
cards;
1 one
2 two
3 three
4 four
; run;

%macro ext_1;
  %let iter=1;
  
  %do %while(&amp;amp;iter &amp;lt;= 4);
     proc sql;
       select word INTO: w from have where num=&amp;amp;iter;
     quit;
     %put W = &amp;amp;w;
     %let iter = %eval(&amp;amp;iter + 1);
  %end;
%mend ext_1;
%ext_1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;attached is the log after running this code.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377447#M90636</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-07-19T16:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377498#M90647</link>
      <description>&lt;P&gt;Your looping seems way too complex. &amp;nbsp;If you know how many observations are in your input dataset then just loop over them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ext_1;
%local iter inputfile ;
%do iter=1 %to &amp;amp;n ;
proc sql noprint; 
select filename into :inputfile trimmed 
  from INP_1
  where rown = &amp;amp;iter.
; 
QUIT;
  %put &amp;amp;=iter &amp;amp;=inputfile;
%end;
%mend ext_1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data INP_1 ;
  rown+1;
  input filename $50.;
cards;
file1
file2
;
%let n=2;
%ext_1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;310  %let n=2;
311  %ext_1;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


ITER=1 INPUTFILE=file1
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


ITER=2 INPUTFILE=file2&lt;/PRE&gt;
&lt;P&gt;If you don't know then let SAS tell you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ext_1;
%local iter inputfile found;
%do %until(not &amp;amp;found);
  %let iter=%eval(&amp;amp;iter+1);
proc sql noprint; 
select filename into :inputfile trimmed 
  from INP_1
  where rown = &amp;amp;iter.
; 
QUIT;
  %let found=&amp;amp;sqlobs;
  %if &amp;amp;found %then %do;
    %put &amp;amp;=iter &amp;amp;=inputfile;
  %end;
%end;
%mend ext_1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 18:23:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/377498#M90647</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-19T18:23:03Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/378159#M90848</link>
      <description>&lt;P&gt;Thanks all for your answers,&lt;/P&gt;&lt;P&gt;I finally found the core of the issue. But I don't understand it...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At the end of my program I logically call the macro ext_1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I do this&lt;/P&gt;&lt;PRE&gt;%ext_1;&lt;/PRE&gt;&lt;P&gt;It's ok.&lt;/P&gt;&lt;P&gt;But I don't do this, I do that:&lt;/P&gt;&lt;PRE&gt;data _null_;
if "&amp;amp;UN." = "YES" then call execute('%ext_1');

run;&lt;/PRE&gt;&lt;P&gt;UN is a macro variable. In my case it is "YES" but it can also be "NO"&lt;/P&gt;&lt;P&gt;And when I write this I have the issue I was talking about.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any explanation why?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2017 14:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/378159#M90848</guid>
      <dc:creator>FP12</dc:creator>
      <dc:date>2017-07-21T14:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL INTO issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/378164#M90850</link>
      <description>&lt;P&gt;When you use a statement like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call execute('%mymacro;');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the SAS macro processor will run the macro and push onto the execution stack the generated SAS code to run AFTER the data step finishes. &amp;nbsp;So any processing that depended on macro varaibles that were generated by PROC SQL code that the macro generated will NOT use the macro variable values that the PROC SQL code will generate when it runs in the future, but any values that already existed when the data step caused the macro to run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wrap the macro call in %nrstr() to prevent this and instead just push the actual macro call onto the stack to run after the data step finishes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if "&amp;amp;UN." = "YES" then call execute('%nrstr(%ext_1);');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Plus it will make your SAS log much more readable since the lines with + at the start that show the lines of code that CALL EXECUTE() added to the stack will now just look like:&lt;/P&gt;
&lt;PRE&gt;+ %ext_1;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2017 14:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-INTO-issue/m-p/378164#M90850</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-21T14:58:04Z</dc:date>
    </item>
  </channel>
</rss>

