<?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 PROC SQL within a macro - Failed to execute properly in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546606#M151358</link>
    <description>&lt;P&gt;Hello there !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm writing a macro that will conditionaly reverse datetimes in char form to datetime in numeric form.&lt;/P&gt;&lt;P&gt;For that, i'm using a mix of data steps and sql procs :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro NUMdatetime(table,var);
	proc sql;
     select type into :type
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit; 
	proc sql;
     select label into :label1
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit;
	%if %symexist(type)=1 %then %do;
   	%if &amp;amp;type.^=char %then %do;
	%put ERROR: Variable is not character.. transformation not possible !!;
	%goto exit;
	%end;
	data SDTM.&amp;amp;table.; set SDTM.&amp;amp;table.;
	   	format &amp;amp;var.2 datetime19.;
		&amp;amp;var.2=input(&amp;amp;var.,anydtdtm.);
		drop &amp;amp;var.;
		rename &amp;amp;var.2=&amp;amp;var.;
	run;
	data SDTM.&amp;amp;table.; set SDTM.&amp;amp;table.;
		label &amp;amp;var.="&amp;amp;label1.";
	run;
	%exit:
	%end; 
%mend NUMdatetime;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue is that SQL part doesn't seem to execute correctly.&lt;/P&gt;&lt;P&gt;My &amp;amp;type var stays the same as it was before the command.&lt;/P&gt;&lt;P&gt;If I execute both parts separatly, the &amp;amp;type variable updates correctly.&lt;/P&gt;&lt;P&gt;I have no error nor warnings messages in log.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to resolve each one in a different macro, but it looks like the problem comes from SQL code inside a macro :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro SQL_EVAL(table,var);
  	proc sql;
     select type into :type
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit; 
	proc sql;
     select label into :label1
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit;
%mend SQL_EVAL;

%SQL_EVAL(AE,AEENDTC);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;13767 %macro SQL_EVAL(table,var);&lt;/P&gt;&lt;P&gt;13768 proc sql;&lt;/P&gt;&lt;P&gt;13769 select type into :type&lt;/P&gt;&lt;P&gt;13770 from dictionary.columns&lt;/P&gt;&lt;P&gt;13771 where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";&lt;/P&gt;&lt;P&gt;13772 quit;&lt;/P&gt;&lt;P&gt;13773 proc sql;&lt;/P&gt;&lt;P&gt;13774 select label into :label1&lt;/P&gt;&lt;P&gt;13775 from dictionary.columns&lt;/P&gt;&lt;P&gt;13776 where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";&lt;/P&gt;&lt;P&gt;13777 quit;&lt;/P&gt;&lt;P&gt;13778 %mend SQL_EVAL;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;13779 %SQL_EVAL(AE,AEENDTC);&lt;/P&gt;&lt;P&gt;NOTE: No rows were selected.&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.01 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;NOTE: No rows were selected.&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.00 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I only use the program, without using it in a macro :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	proc sql;
     select type into :type
     from dictionary.columns
     where memname='AE' and libname='SDTM' and name="AEENDTC";
	quit; 
	proc sql;
     select label into :label1
     from dictionary.columns
     where memname='AE' and libname='SDTM' and name="AEENDTC";
	quit;
	%put &amp;amp;type.;
	%put &amp;amp;label1.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;13781 proc sql;&lt;/P&gt;&lt;P&gt;13782 select type into :type&lt;/P&gt;&lt;P&gt;13783 from dictionary.columns&lt;/P&gt;&lt;P&gt;13784 where memname='AE' and libname='SDTM' and name="AEENDTC";&lt;/P&gt;&lt;P&gt;13785 quit;&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.07 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.03 seconds&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;13786 proc sql;&lt;/P&gt;&lt;P&gt;13787 select label into :label1&lt;/P&gt;&lt;P&gt;13788 from dictionary.columns&lt;/P&gt;&lt;P&gt;13789 where memname='AE' and libname='SDTM' and name="AEENDTC";&lt;/P&gt;&lt;P&gt;13790 quit;&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.01 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;13791 %put &amp;amp;type.;&lt;/P&gt;&lt;P&gt;num&lt;/P&gt;&lt;P&gt;13792 %put &amp;amp;label1.;&lt;/P&gt;&lt;P&gt;End Date/Time of Adverse Event&lt;/P&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;And there is works, the type was numeric at this point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas ?&lt;/P&gt;&lt;P&gt;Thanks for reading !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Mar 2019 17:22:09 GMT</pubDate>
    <dc:creator>Althaea</dc:creator>
    <dc:date>2019-03-27T17:22:09Z</dc:date>
    <item>
      <title>PROC SQL within a macro - Failed to execute properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546606#M151358</link>
      <description>&lt;P&gt;Hello there !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm writing a macro that will conditionaly reverse datetimes in char form to datetime in numeric form.&lt;/P&gt;&lt;P&gt;For that, i'm using a mix of data steps and sql procs :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro NUMdatetime(table,var);
	proc sql;
     select type into :type
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit; 
	proc sql;
     select label into :label1
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit;
	%if %symexist(type)=1 %then %do;
   	%if &amp;amp;type.^=char %then %do;
	%put ERROR: Variable is not character.. transformation not possible !!;
	%goto exit;
	%end;
	data SDTM.&amp;amp;table.; set SDTM.&amp;amp;table.;
	   	format &amp;amp;var.2 datetime19.;
		&amp;amp;var.2=input(&amp;amp;var.,anydtdtm.);
		drop &amp;amp;var.;
		rename &amp;amp;var.2=&amp;amp;var.;
	run;
	data SDTM.&amp;amp;table.; set SDTM.&amp;amp;table.;
		label &amp;amp;var.="&amp;amp;label1.";
	run;
	%exit:
	%end; 
%mend NUMdatetime;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue is that SQL part doesn't seem to execute correctly.&lt;/P&gt;&lt;P&gt;My &amp;amp;type var stays the same as it was before the command.&lt;/P&gt;&lt;P&gt;If I execute both parts separatly, the &amp;amp;type variable updates correctly.&lt;/P&gt;&lt;P&gt;I have no error nor warnings messages in log.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to resolve each one in a different macro, but it looks like the problem comes from SQL code inside a macro :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro SQL_EVAL(table,var);
  	proc sql;
     select type into :type
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit; 
	proc sql;
     select label into :label1
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";
	quit;
%mend SQL_EVAL;

%SQL_EVAL(AE,AEENDTC);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;13767 %macro SQL_EVAL(table,var);&lt;/P&gt;&lt;P&gt;13768 proc sql;&lt;/P&gt;&lt;P&gt;13769 select type into :type&lt;/P&gt;&lt;P&gt;13770 from dictionary.columns&lt;/P&gt;&lt;P&gt;13771 where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";&lt;/P&gt;&lt;P&gt;13772 quit;&lt;/P&gt;&lt;P&gt;13773 proc sql;&lt;/P&gt;&lt;P&gt;13774 select label into :label1&lt;/P&gt;&lt;P&gt;13775 from dictionary.columns&lt;/P&gt;&lt;P&gt;13776 where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var.";&lt;/P&gt;&lt;P&gt;13777 quit;&lt;/P&gt;&lt;P&gt;13778 %mend SQL_EVAL;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;13779 %SQL_EVAL(AE,AEENDTC);&lt;/P&gt;&lt;P&gt;NOTE: No rows were selected.&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.01 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;NOTE: No rows were selected.&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.00 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I only use the program, without using it in a macro :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	proc sql;
     select type into :type
     from dictionary.columns
     where memname='AE' and libname='SDTM' and name="AEENDTC";
	quit; 
	proc sql;
     select label into :label1
     from dictionary.columns
     where memname='AE' and libname='SDTM' and name="AEENDTC";
	quit;
	%put &amp;amp;type.;
	%put &amp;amp;label1.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;13781 proc sql;&lt;/P&gt;&lt;P&gt;13782 select type into :type&lt;/P&gt;&lt;P&gt;13783 from dictionary.columns&lt;/P&gt;&lt;P&gt;13784 where memname='AE' and libname='SDTM' and name="AEENDTC";&lt;/P&gt;&lt;P&gt;13785 quit;&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.07 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.03 seconds&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;13786 proc sql;&lt;/P&gt;&lt;P&gt;13787 select label into :label1&lt;/P&gt;&lt;P&gt;13788 from dictionary.columns&lt;/P&gt;&lt;P&gt;13789 where memname='AE' and libname='SDTM' and name="AEENDTC";&lt;/P&gt;&lt;P&gt;13790 quit;&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.01 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;13791 %put &amp;amp;type.;&lt;/P&gt;&lt;P&gt;num&lt;/P&gt;&lt;P&gt;13792 %put &amp;amp;label1.;&lt;/P&gt;&lt;P&gt;End Date/Time of Adverse Event&lt;/P&gt;&lt;P&gt;----------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;And there is works, the type was numeric at this point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas ?&lt;/P&gt;&lt;P&gt;Thanks for reading !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2019 17:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546606#M151358</guid>
      <dc:creator>Althaea</dc:creator>
      <dc:date>2019-03-27T17:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL within a macro - Failed to execute properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546613#M151362</link>
      <description>&lt;P&gt;Macro triggers do not resolve when enclosed in single quotes. Use double quotes instead.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2019 17:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546613#M151362</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-27T17:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL within a macro - Failed to execute properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546615#M151363</link>
      <description>&lt;P&gt;Most likely the issue you are having is that INTO will not create the macro variable if no observations meet the where clause.&lt;/P&gt;
&lt;P&gt;Either test the SQLOBS automatic variable and/or set a default value before the select.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
%let type=;
   select type into :type
     from dictionary.columns
     where memname='&amp;amp;table.' and libname='SDTM' and name="&amp;amp;var."
   ;
%if &amp;amp;sqlobs %then ....
%if %length(&amp;amp;type) %then ...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Especially if you are trying to find a table with an &amp;amp; in the member name.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    where memname=%upcase("&amp;amp;table.") and libname='SDTM' and upcase(name)=%upcase("&amp;amp;var.")&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Mar 2019 17:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546615#M151363</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-27T17:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL within a macro - Failed to execute properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546830#M151474</link>
      <description>&lt;P&gt;After fixing double quotes the program works fine !&lt;/P&gt;&lt;P&gt;Thanks for your help ! &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2019 09:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-within-a-macro-Failed-to-execute-properly/m-p/546830#M151474</guid>
      <dc:creator>Althaea</dc:creator>
      <dc:date>2019-03-28T09:23:08Z</dc:date>
    </item>
  </channel>
</rss>

