<?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 return a value from a macro called from a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731675#M227923</link>
    <description>&lt;P&gt;I am doing a call execute from a data set to execute a macro and return the gsqlrc(which contains sqlxrc) and gsqlmsg(which contains sqlxmsg).&amp;nbsp; However, the 2 assignment statements are getting a null value. I look at the value in the macro and it is not null.&amp;nbsp; Any ideas?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final_validation;
	set dbtblnames_complete;
	tablename=newtblname1;
	call execute('%nrstr(%deterifcolsexist(&amp;amp;var1,&amp;amp;var2,' || strip(newtblname1) || '));');
&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;	call execute('%nrstr(%tstuniqvalid(' || strip(newtblname1) || '))');
	sqlerrcode=symget('gsqlrc');
	sqlerrmsg=symget('gsqlmsg');&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 06 Apr 2021 19:16:52 GMT</pubDate>
    <dc:creator>Gayle</dc:creator>
    <dc:date>2021-04-06T19:16:52Z</dc:date>
    <item>
      <title>how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731675#M227923</link>
      <description>&lt;P&gt;I am doing a call execute from a data set to execute a macro and return the gsqlrc(which contains sqlxrc) and gsqlmsg(which contains sqlxmsg).&amp;nbsp; However, the 2 assignment statements are getting a null value. I look at the value in the macro and it is not null.&amp;nbsp; Any ideas?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final_validation;
	set dbtblnames_complete;
	tablename=newtblname1;
	call execute('%nrstr(%deterifcolsexist(&amp;amp;var1,&amp;amp;var2,' || strip(newtblname1) || '));');
&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;	call execute('%nrstr(%tstuniqvalid(' || strip(newtblname1) || '))');
	sqlerrcode=symget('gsqlrc');
	sqlerrmsg=symget('gsqlmsg');&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 19:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731675#M227923</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-06T19:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731676#M227924</link>
      <description>&lt;P&gt;Call Execute stacks code for execution after the Data Step terminates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably need the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p09dcftd1xxg1kn1brnjyc0q93yk.htm" target="_self"&gt;Dosubl Function&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 19:18:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731676#M227924</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-04-06T19:18:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731677#M227925</link>
      <description>CALL EXECUTE only executes at the end of a data step, so you cannot use the values returned within the data step. You could replace it with DOSUBL which will execute immediately. &lt;BR /&gt;Or revisit your program and see if a function via PROC FCMP would be more appropriate than a macro.</description>
      <pubDate>Tue, 06 Apr 2021 19:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731677#M227925</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-06T19:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731678#M227926</link>
      <description>&lt;P&gt;There is no way for the macro to return anything to the current data step since the macro will not even start running until after the data step finishes running.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 19:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731678#M227926</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-06T19:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731691#M227935</link>
      <description>&lt;P&gt;Just what are those macros supposed to accomplish?&lt;/P&gt;
&lt;P&gt;Without having the definition of the macros it may be that you also have a scope issue of expecting a macro variable to be available outside of the macro using/creating it but is only a local macro variable.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 21:13:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731691#M227935</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-06T21:13:55Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731694#M227938</link>
      <description>&lt;P&gt;Thank you .&amp;nbsp; I did use DOSUBL but i get&amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;ERROR 180-322: Statement is not valid or it is used out of proper orde&lt;/FONT&gt;&lt;/STRONG&gt;r. do you see the error?&amp;nbsp; I have not figured it out.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final_validation;
	set dbtblnames_complete;
	tablename=newtblname1;
	call execute('%nrstr(%deterifcolsexist(&amp;amp;var1,&amp;amp;var2,' || strip(newtblname1) || '));');
	 &lt;FONT color="#000080"&gt;&lt;STRONG&gt;rc = dosubl('%nrstr(%tstuniqvalid(' || strip(newtblname1) || '))');&lt;/STRONG&gt;&lt;/FONT&gt;
	/*%put &amp;amp;gsqlrc &amp;amp;gsqlmsg;*/;
	sqlerrcode=symget('gsqlrc');
	sqlerrmsg=symget('gsqlmsg');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 19:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731694#M227938</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-06T19:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731695#M227939</link>
      <description>My guess is your other CALL EXECUTE also needs to be a DOSUBL. Does anything happen there that's required for the macro after?</description>
      <pubDate>Tue, 06 Apr 2021 20:02:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731695#M227939</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-06T20:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731754#M227970</link>
      <description>&lt;P&gt;no nothing that is there in that call is required for the macro&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 00:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731754#M227970</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T00:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731756#M227971</link>
      <description>well it looks like it did not like the nrstr.  I removed that however I still am not getting the expected return values .  I  get nulls(.)</description>
      <pubDate>Wed, 07 Apr 2021 00:26:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731756#M227971</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T00:26:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731786#M227982</link>
      <description>Time to show the rest of your code or a reproducible example.</description>
      <pubDate>Wed, 07 Apr 2021 02:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731786#M227982</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-07T02:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731908#M228030</link>
      <description>I  made the variable global</description>
      <pubDate>Wed, 07 Apr 2021 13:47:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731908#M228030</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T13:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731912#M228033</link>
      <description>&lt;P&gt;Start with something simple, and expand from there:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test1(param);
%global &amp;amp;param.;
%let &amp;amp;param. = YYY;
%mend;

data _null_;
rc = dosubl('%test1(XXX)');
xxx = symget('XXX');
put xxx=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will see that the DOSUBL method works in creating the macro variables, so that it can be returned by SYMGET.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 13:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731912#M228033</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-07T13:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731914#M228035</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/367603"&gt;@Gayle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;well it looks like it did not like the nrstr. I removed that however I still am not getting the expected return values . I get nulls(.)&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, tested it, DOSUBL does not like the %NRSTR. %NRSTR is also only needed in CALL EXECUTE to prevent premature execution of macro code.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 14:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731914#M228035</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-07T14:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731930#M228042</link>
      <description>&lt;P&gt;ok here is what I am trying to accomplish:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This macro gets called:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tstuniqvalid(crnttblname);
proc sql;
connect to teradata as xxxx (username=&amp;amp;xxuser. password=&amp;amp;xxpass. tdpid=&amp;amp;xxWP_TDPID. mode=teradata fastload=yes);
create table unique_validcustid as
select * from connection to xxxx (
select  hist_ky_add
		,count(*)
from (select cast(cust_id as decimal(15,0)) as cust_id_test
	,cust_id 
	,cust_id_type 
	,cell_ky
	,rank(cust_id || cust_id_type || cast(cell_ky as varchar(16))asc)as hist_ky_add
from &amp;amp;crnttblname) a
group by 1
having count(*) &amp;gt; 1
);

%global gsqlrc gsqlmsg;

%if &amp;amp;sqlxrc ne 0 %then
   %do;
	   %put "Test load table for uniqueness and valid cust_id FAILED due to SQL error";
	   %let gsqlrc=&amp;amp;sqlxrc;
	   /*%put &amp;amp;=gsqlrc;*/
	   %let gsqlmsg=&amp;amp;sqlxmsg;
	   /*%put &amp;amp;=gsqlmsg;*/
	   data _null_;
        call symput('gsqlrc',gsqlrc);
        call symput('gsqlmsg',gsqlmsg);
	    %put "in the data step of tstuniqu";
	    %put &amp;amp;=gsqlrc "rc in the data step";
	    %put &amp;amp;=gsqlmsg "msg in the data step";
	   run;
   %end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and what i am trying to do in it is return the sql return code and message when it is not 0 (i e there is a sql error) . So that i can put it in a table.It is called from here:&amp;nbsp; The symgets are where I am trying to assign the returned values to a table(final_validation);&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final_validation;
	set dbtblnames_complete;
	tablename=newtblname1;
	call execute('%nrstr(%tstuniqvalid(' || strip(newtblname1) || '))');
       rc = dosubl('%tstuniqvalid(' || strip(newtblname1) || ')');
	sqlerrcode=symget('gsqlrc');        
	sqlerrmsg=symget('gsqlmsg');
	put "Put data in the table";
	put sqlerrcode;
	put sqlerrmsg;  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Apr 2021 14:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731930#M228042</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T14:24:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731932#M228044</link>
      <description>Why do you have both a call execute and a DOSUBL?&lt;BR /&gt;Leave in only the DOSUBL.&lt;BR /&gt;&lt;BR /&gt;I'm assuming you also just missed the %MEND end for your macro?</description>
      <pubDate>Wed, 07 Apr 2021 14:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731932#M228044</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-07T14:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731950#M228056</link>
      <description>&lt;P&gt;hi thank you the %MEND is there just did not put it in the post.&amp;nbsp; I will make the other DOSUBL but I would not think one impacts the other, they are separate ......&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 15:16:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/731950#M228056</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T15:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/732001#M228087</link>
      <description>Thank you Reeza I will use DOSUBL instead of call execute</description>
      <pubDate>Wed, 07 Apr 2021 18:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/732001#M228087</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T18:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/732006#M228089</link>
      <description>&lt;P&gt;thank you!&amp;nbsp; I pared it down and now I seem to be getting what I expect.&amp;nbsp; I had symput in the macro and I guess I was unclear on how it works , removing it helped&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 18:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/732006#M228089</guid>
      <dc:creator>Gayle</dc:creator>
      <dc:date>2021-04-07T18:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to return a value from a macro called from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/732009#M228092</link>
      <description>&lt;P&gt;Passing values through macro variables is probable not what you want to do.&amp;nbsp; Just have your macro insert observations into a table.&amp;nbsp; You can then use the values from that table after all of the macro calls have finished.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tstuniqvalid(crnttblname);
proc sql;
connect using xxxx ;
create table unique_validcustid as
select * from connection to xxxx (
select  hist_ky_add
		,count(*)
from (select cast(cust_id as decimal(15,0)) as cust_id_test
	,cust_id 
	,cust_id_type 
	,cell_ky
	,rank(cust_id || cust_id_type || cast(cell_ky as varchar(16))asc)as hist_ky_add
from &amp;amp;crnttblname) a
group by 1
having count(*) &amp;gt; 1
);

insert into final_validation(crnttblname,nobs,sqlxrc,sqlxmsg)
  values ( "&amp;amp;crnttblname",&amp;amp;sqlobs,&amp;amp;sqlxrc,"%superq(sqlxmsg)" )
;
quit;
%mend;


* Connect to teradata ;
libname xxxx teradata username=&amp;amp;xxuser. password=&amp;amp;xxpass. tdpid=&amp;amp;xxWP_TDPID. mode=teradata fastload=yes;

* Create empty summary table ;
data final_validation;
  length crnttblname $128 nobs 8 sqlxrc 8 sqlxmsg $256 ;
  stop;
run;

* Call macro once for each table ;
data _null_;
  set dbtblnames_complete;
  call execute(cats('%nrstr(%tstuniqvalid)(',newtblname1,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can now just print the dataset if you want to see any tables that caused errors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=final_validation;
  where sqlxrc ne 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Apr 2021 18:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-return-a-value-from-a-macro-called-from-a-data-step/m-p/732009#M228092</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-07T18:22:30Z</dc:date>
    </item>
  </channel>
</rss>

