<?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: pass a dynamic variable value as the data set  name for Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18438#M2765</link>
    <description>As Linus already said: &lt;BR /&gt;
You have to put your macro definition in front of the data step. The way the code is designed now you're calling the macro before you compile it. &lt;BR /&gt;
If you're running your code in batch then it will never work, if it's a session then the code will fail the first time but then run in all consecutive trials (as now the macro got compiled into the sas macro catalogue in WORK).</description>
    <pubDate>Thu, 19 Nov 2009 23:11:49 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2009-11-19T23:11:49Z</dc:date>
    <item>
      <title>pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18430#M2757</link>
      <description>Hi,&lt;BR /&gt;
I have a dynamic dataset which can be grouped by var1 and var2. I have to split the dataset to a number of different data set based on the group of var1 and var2.&lt;BR /&gt;
After I split them, I need to do some data manipulation for each of those data set. since the name of those data sets are dynamic, I was trying to use a variable to get the name of the data set and pass it to the macro for data manipulation, but I couldn't find the right way to pass a variable as the data set name in the Macro. &lt;BR /&gt;
Here is my sample code:&lt;BR /&gt;
data _null_ ;&lt;BR /&gt;
    set Overall;&lt;BR /&gt;
	subGroup="g" ||compress(var1,' ') ||"c"||compress(var2,"-") ;     &lt;BR /&gt;
	call execute ( "data " ||  subgroup||";" || &lt;BR /&gt;
         "set Overall ( where =( group = " || put(var1,1.) || " and centre = '" || put(var2,7.) || "'));" ||&lt;BR /&gt;
         "run ;" ) ;&lt;BR /&gt;
	%CalByCentre(subgroup);**********&lt;B&gt;Subgroup won't be resolved&lt;/B&gt;&lt;BR /&gt;
run ;&lt;BR /&gt;
%Macro CalByCentre(&amp;amp;dsn);&lt;BR /&gt;
data subVis;	&lt;BR /&gt;
set &amp;amp;dsn;&lt;BR /&gt;
if sub_visit=1 and group=1 then do;&lt;BR /&gt;
	output LTsubVis;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend CalbyCentre;&lt;BR /&gt;
&lt;BR /&gt;
Any help will be really appreciated! &lt;BR /&gt;
Cathy</description>
      <pubDate>Tue, 17 Nov 2009 19:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18430#M2757</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-11-17T19:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18431#M2758</link>
      <description>Hi Cathy&lt;BR /&gt;
&lt;BR /&gt;
There are so many issues with the code you provide that I wouldn't know where to start.&lt;BR /&gt;
&lt;BR /&gt;
I thought it might be more helpful to give you a code example which could serve you as starting point to code what you need.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  do var1=1 to 3;&lt;BR /&gt;
    do var2=1 to 2;&lt;BR /&gt;
      othervar=var1*var2;&lt;BR /&gt;
      output;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* create macro var with list of dataset names */&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select distinct cats('DS_',var1,'_',var2) into :DSList separated by ' '&lt;BR /&gt;
    from have;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
/* macro creating SAS code block to output data into selected target data sets */&lt;BR /&gt;
%macro SelectDS;&lt;BR /&gt;
  %let i=1;&lt;BR /&gt;
  %do %while (%scan(&amp;amp;DSList,&amp;amp;i,' ') ne );&lt;BR /&gt;
    %let DSname=%scan(&amp;amp;DSList,&amp;amp;i,' ');&lt;BR /&gt;
    if var1=%scan(&amp;amp;DSname,2,'_') and var2=%scan(&amp;amp;DSname,3,'_') then output &amp;amp;DSname %str(;) &lt;BR /&gt;
    else&lt;BR /&gt;
    %let i=%eval(&amp;amp;i+1);&lt;BR /&gt;
  %end;&lt;BR /&gt;
&lt;BR /&gt;
  /* finish if... then... else... block and catch undefined cases */&lt;BR /&gt;
  output UndefinedCases;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/* process source data and output into target data sets */&lt;BR /&gt;
data &amp;amp;DSList UndefinedCases;&lt;BR /&gt;
  set have;&lt;BR /&gt;
  /* do some processing here */&lt;BR /&gt;
  /* ....................&lt;BR /&gt;
     ....................  */&lt;BR /&gt;
&lt;BR /&gt;
  /* generate SAS statements for output to target data sets */&lt;BR /&gt;
  %SelectDS&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 17 Nov 2009 23:26:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18431#M2758</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-11-17T23:26:16Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18432#M2759</link>
      <description>I think, the problem is here:&lt;BR /&gt;
&lt;BR /&gt;
When macro starts, the table subgroup is not still created, because call execute submits only after "run;" statement.&lt;BR /&gt;
&lt;BR /&gt;
Try this:&lt;BR /&gt;
data _null_ ;&lt;BR /&gt;
set Overall;&lt;BR /&gt;
subGroup="g" ||compress(var1,' ') ||"c"||compress(var2,"-") ;&lt;BR /&gt;
call execute ( "data " || subgroup||";" ||&lt;BR /&gt;
"set Overall ( where =( group = " || put(var1,1.) || " and centre = '" || put(var2,7.) || "'));" ||&lt;BR /&gt;
"run ;" ) ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
%CalByCentre(subgroup);**********Subgroup won't be resolved;</description>
      <pubDate>Wed, 18 Nov 2009 08:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18432#M2759</guid>
      <dc:creator>SAS_user</dc:creator>
      <dc:date>2009-11-18T08:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18433#M2760</link>
      <description>1) Put your macro definition in front of the data step&lt;BR /&gt;
2) Macro calls won't execute within a data step. So you need to use call execute for that one as well: call execute("%CalByCentre("||subgroup||");");&lt;BR /&gt;
/Linus</description>
      <pubDate>Wed, 18 Nov 2009 13:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18433#M2760</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-11-18T13:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18434#M2761</link>
      <description>Actually the macro call would execute where it is, but the result would be far from what is expected.&lt;BR /&gt;
&lt;BR /&gt;
If we resolve the first pass at the macro we would get:&lt;BR /&gt;
data _null_ ;&lt;BR /&gt;
set Overall;&lt;BR /&gt;
subGroup="g" ||compress(var1,' ') ||"c"||compress(var2,"-") ; &lt;BR /&gt;
call execute ( "data " || subgroup||";" || &lt;BR /&gt;
"set Overall ( where =( group = " || put(var1,1.) || " and centre = '" || put(var2,7.) || "'));" ||&lt;BR /&gt;
"run ;" ) ;&lt;BR /&gt;
***********   SAS would assume a "RUN;" here.&lt;BR /&gt;
data subVis; &lt;BR /&gt;
set subgroup;&lt;BR /&gt;
if sub_visit=1 and group=1 then do;&lt;BR /&gt;
output LTsubVis;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
so the SAS system would see a second Data step, assume a run; stop the execution of the Data _null_ ; and process data subVis.  The macro would process there if it contained code which would execute within the data _null_.</description>
      <pubDate>Wed, 18 Nov 2009 13:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18434#M2761</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-11-18T13:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18435#M2762</link>
      <description>Thanks for all the help. &lt;BR /&gt;
I think Linus is right, I need to do "call execute ", otherwise the Macro only run once inside the data step. After I used the "Call execute", it worked for me. But this morning, when I tried to run again, I got an error message" The quoted string currently being processed has become more than 262 haracters long.  You may have  unbalanced quotation marks". It's the same program worked for me yesterday, any thought about this?&lt;BR /&gt;
Thanks.&lt;BR /&gt;
&lt;BR /&gt;
Cathy</description>
      <pubDate>Thu, 19 Nov 2009 15:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18435#M2762</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-11-19T15:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18436#M2763</link>
      <description>When creating Call Execute statements it is quite often usefull to create a string first so that you can examine exactly what you are sending to the call execute.&lt;BR /&gt;
&lt;BR /&gt;
You can do this by createing a temporary variable and using CALL EXECUTE(tempver);&lt;BR /&gt;
&lt;BR /&gt;
or just do it in development and copy the code into the call execute.</description>
      <pubDate>Thu, 19 Nov 2009 15:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18436#M2763</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-11-19T15:46:20Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18437#M2764</link>
      <description>Thanks for the tips. That will help to solve the string definitely.&lt;BR /&gt;
I think I knew my problem about that error message is because for some reason my Macro wasn't considered end even though I do have "%mend; " statement somewhere. Because the macro wasn't end so the other data steps outside are still considered as part of the Macro. very Weird.</description>
      <pubDate>Thu, 19 Nov 2009 17:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18437#M2764</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-11-19T17:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: pass a dynamic variable value as the data set  name for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18438#M2765</link>
      <description>As Linus already said: &lt;BR /&gt;
You have to put your macro definition in front of the data step. The way the code is designed now you're calling the macro before you compile it. &lt;BR /&gt;
If you're running your code in batch then it will never work, if it's a session then the code will fail the first time but then run in all consecutive trials (as now the macro got compiled into the sas macro catalogue in WORK).</description>
      <pubDate>Thu, 19 Nov 2009 23:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pass-a-dynamic-variable-value-as-the-data-set-name-for-Macro/m-p/18438#M2765</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-11-19T23:11:49Z</dc:date>
    </item>
  </channel>
</rss>

