<?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 How to call a macro with proc sql inside another pros sql? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520307#M4013</link>
    <description>&lt;P&gt;Hi!&amp;nbsp;I&amp;nbsp;want to create a macro which compare two words and which I can call inside proc sql. The problem is that my macro has another proc sql and conflict with the first.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Macro for finding a count of different positions in two words:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table;
   input a;
   datalines; 
1
;run;

%macro symbol_diff_cnt(word1=, word2=);
	%global diff_cnt;

	proc sql;
		select max(length(&amp;amp;word1),length(&amp;amp;word2))
		into: length_max 
		from table
	;quit;
	%let length_max=&amp;amp;length_max;

	%macro test;

		proc sql;
			select 
				0 %do i=1 %to &amp;amp;length_max.;	
					+ (case when substring(&amp;amp;word1 from &amp;amp;i for 1)=substring(&amp;amp;word2 from &amp;amp;i for 1) then 0 else 1 end)
				%end;
			into: dif_cnt 
			from table
		;quit;

	%mend test;

	%test

	%let diff_cnt=&amp;amp;dif_cnt;
	%put &amp;amp;diff_cnt;
	
%mend symbol_diff_cnt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I call this macro inside next trivial proc sql which print two words only if difference between these words &amp;lt; or = 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data words;
   input word $ n;
   datalines;
abcdef 1
abcpef 2
;run;


proc sql;
create table wwww as
	select 
		t1.word as s1,
		t2.word as s2
	from 		words(where=(n=1)) 	as t1
		left join 	words(where=(n=2)) 	as t2	on %symbol_diff_cnt(word1=w1.word,word2=w2.word)&amp;lt;=1
;quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it has errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- because of proc sql:&lt;/P&gt;&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, *, **, +, ',', -, '.', /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND,&lt;BR /&gt;CONTAINS, EQ, EQT, GE, GET, GROUP, GT, GTT, HAVING, LE, LET, LIKE, LT, LTT, NE, NET, OR, ORDER, WHERE, ^=, |, ||,&lt;BR /&gt;~=.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- because of substitution w1.word and w2.word in max(length(w1.word),length(w2.word)):&lt;/P&gt;&lt;P&gt;ERROR: Unresolved reference to table/correlation name w1.&lt;BR /&gt;ERROR: Unresolved reference to table/correlation name w2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- and other:&lt;/P&gt;&lt;P&gt;ERROR: The text expression &amp;amp;LENGTH_MAX contains a recursive reference to the macro variable LENGTH_MAX. The macro variable will be assigned the null value.&lt;BR /&gt;ERROR: %EVAL function has no expression to evaluate, or %IF statement has no condition.&lt;BR /&gt;ERROR: The %TO value of the %DO I loop is invalid.&lt;BR /&gt;ERROR: The macro TEST will stop executing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How I can correct my macro?&lt;/P&gt;</description>
    <pubDate>Tue, 11 Dec 2018 09:53:09 GMT</pubDate>
    <dc:creator>elessar</dc:creator>
    <dc:date>2018-12-11T09:53:09Z</dc:date>
    <item>
      <title>How to call a macro with proc sql inside another pros sql?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520307#M4013</link>
      <description>&lt;P&gt;Hi!&amp;nbsp;I&amp;nbsp;want to create a macro which compare two words and which I can call inside proc sql. The problem is that my macro has another proc sql and conflict with the first.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Macro for finding a count of different positions in two words:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table;
   input a;
   datalines; 
1
;run;

%macro symbol_diff_cnt(word1=, word2=);
	%global diff_cnt;

	proc sql;
		select max(length(&amp;amp;word1),length(&amp;amp;word2))
		into: length_max 
		from table
	;quit;
	%let length_max=&amp;amp;length_max;

	%macro test;

		proc sql;
			select 
				0 %do i=1 %to &amp;amp;length_max.;	
					+ (case when substring(&amp;amp;word1 from &amp;amp;i for 1)=substring(&amp;amp;word2 from &amp;amp;i for 1) then 0 else 1 end)
				%end;
			into: dif_cnt 
			from table
		;quit;

	%mend test;

	%test

	%let diff_cnt=&amp;amp;dif_cnt;
	%put &amp;amp;diff_cnt;
	
%mend symbol_diff_cnt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I call this macro inside next trivial proc sql which print two words only if difference between these words &amp;lt; or = 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data words;
   input word $ n;
   datalines;
abcdef 1
abcpef 2
;run;


proc sql;
create table wwww as
	select 
		t1.word as s1,
		t2.word as s2
	from 		words(where=(n=1)) 	as t1
		left join 	words(where=(n=2)) 	as t2	on %symbol_diff_cnt(word1=w1.word,word2=w2.word)&amp;lt;=1
;quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it has errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- because of proc sql:&lt;/P&gt;&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, *, **, +, ',', -, '.', /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND,&lt;BR /&gt;CONTAINS, EQ, EQT, GE, GET, GROUP, GT, GTT, HAVING, LE, LET, LIKE, LT, LTT, NE, NET, OR, ORDER, WHERE, ^=, |, ||,&lt;BR /&gt;~=.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- because of substitution w1.word and w2.word in max(length(w1.word),length(w2.word)):&lt;/P&gt;&lt;P&gt;ERROR: Unresolved reference to table/correlation name w1.&lt;BR /&gt;ERROR: Unresolved reference to table/correlation name w2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- and other:&lt;/P&gt;&lt;P&gt;ERROR: The text expression &amp;amp;LENGTH_MAX contains a recursive reference to the macro variable LENGTH_MAX. The macro variable will be assigned the null value.&lt;BR /&gt;ERROR: %EVAL function has no expression to evaluate, or %IF statement has no condition.&lt;BR /&gt;ERROR: The %TO value of the %DO I loop is invalid.&lt;BR /&gt;ERROR: The macro TEST will stop executing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How I can correct my macro?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 09:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520307#M4013</guid>
      <dc:creator>elessar</dc:creator>
      <dc:date>2018-12-11T09:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to call a macro with proc sql inside another pros sql?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520352#M4017</link>
      <description>&lt;P&gt;Simply described, macro is a code generator.&lt;/P&gt;
&lt;P&gt;It means that the generated code must fit into the environment that calls it.&lt;/P&gt;
&lt;P&gt;Since SQL doesn't allow calls to a separate PROC SQL session, this can't be done using a macro either.&lt;/P&gt;
&lt;P&gt;I'm not sure what you try to accomplish (please describe using plain language), but bottom line is tyou can't execute code in the macro that query data, you need a way to resolve this without using PROC or data step (function calls using %sysfunc is generally allowed),&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 13:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520352#M4017</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-12-11T13:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to call a macro with proc sql inside another pros sql?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520361#M4018</link>
      <description>&lt;P&gt;You don't.&amp;nbsp; You cannot nest data steps/procedure steps inside of each other.&amp;nbsp; When SAS sees the start of a new step it finishes compiling the current step and runs it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also do not place the macro definitions inside of each other.&amp;nbsp; It does nothing other than confuse the programmer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your program does not need to start and stop PROC SQL so many times. Just have your subroutine macro generate the SELECT statement only.&amp;nbsp; Then only call it when already running PROC SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
%local i ;
   select
    0 %do i=1 %to &amp;amp;length_max.;
     + (case when substring(&amp;amp;word1 from &amp;amp;i for 1)=substring(&amp;amp;word2 from &amp;amp;i for 1) then 0 else 1 end)
    %end;
   into :diff_cnt trimmed
   from table
  ;
%mend test;
%macro symbol_diff_cnt(word1=, word2=);
%global diff_cnt;

proc sql noprint ;
  select max(length(&amp;amp;word1),length(&amp;amp;word2))
    into :length_max trimmed
    from table
  ;

 %test

 %put &amp;amp;diff_cnt;
quit;

%mend symbol_diff_cnt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However the logic of your SQL also looks strange.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you using that strange SUBSTRING() function syntax instead of using the normal SUBSTR() or SUBSTRN() function?&amp;nbsp; Are you pushing this code into a remote database?&lt;/P&gt;
&lt;P&gt;Why are you calculating the maximum length of the new variables for just the first observations from TABLE?&amp;nbsp; Did you mean to call the SQL aggregate function MAX() in addition to the SAS statistical function MAX(,)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thing to always remember with macro code is that it is mainly used to generate code. So before trying to create a macro it is important to first get the SAS code that you want to generate figured out. Then you can evaluate if you can generate that code using a macro or not.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 13:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520361#M4018</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-11T13:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to call a macro with proc sql inside another pros sql?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520363#M4019</link>
      <description>&lt;P&gt;Please explain what you are trying to do.&amp;nbsp; This really doesn't look like a problem that needs either macro code or SQL code.&amp;nbsp; It probably could be done much easier in a data step.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 13:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520363#M4019</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-11T13:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to call a macro with proc sql inside another pros sql?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520745#M4079</link>
      <description>&lt;P&gt;Hi! I rewrote code for macro without proc sql:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro symbol_diff_cnt(word1=, word2=);

	%let buffer = 0;

	%do i=1 %to %sysfunc(max(%length(&amp;amp;word1),%length(&amp;amp;word2)));	
		%if (%qsubstr(&amp;amp;word1, &amp;amp;i, 1) ^= %qsubstr(&amp;amp;word2, &amp;amp;i, 1)) %then %do;
			%let buffer = %eval(&amp;amp;buffer + 1);
		%end;
	%end; 

	&amp;amp;buffer;

%mend symbol_diff_cnt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and it works if I call it by strings like that (in this example difference between meanings of word1 and word2 equals 2):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aaa;
   input a;
   datalines;
1
;run;

%let new = %symbol_diff_cnt(word1='abcdef',word2='adcpef');
%put new = &amp;amp;new;

proc sql;
	create table SSS as
	select 
		'abcdef'	as s1,
		'adcpef'	as s2,
		&amp;amp;new.	as dif
	from aaa
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But if I substitute variables, macro compare its names, but not its meanings (in this example difference between names "t1.word" and "t2.word" equals 1):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data words;
   input word $ n;
   datalines;
abcdef 1
adcpef 2
;run;

%let neww = %symbol_diff_cnt(word1=t1.word,word2=t2.word);
%put neww = &amp;amp;neww;

proc sql;
	create table TTT as
	select 
		t1.word as slovo1,
		t2.word as slovo2,
		&amp;amp;neww.				as diff
	from words(where=(n=1))				t1
		left join words(where=(n=2))	t2	on t2.n=t1.n+1
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I force macro to compare meanings of variables, not its names?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 11:23:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-call-a-macro-with-proc-sql-inside-another-pros-sql/m-p/520745#M4079</guid>
      <dc:creator>elessar</dc:creator>
      <dc:date>2018-12-12T11:23:44Z</dc:date>
    </item>
  </channel>
</rss>

