<?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: Assigning value to a macro variable within a datastep (without call symput) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809402#M319191</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/241108"&gt;@Rain_28&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Memname is actually a dataset name and dates is actual date variable (that I am extracting from _var1).&lt;/P&gt;
&lt;P&gt;want to put Dates in macro variable and call datasets with call execute. As the loop iterates want to pick memname and correspondaing dates variable from that memname.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;MEMNAME&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;_var1&lt;/TD&gt;
&lt;TD&gt;dates&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;XY&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XYSTDAT_1&lt;/TD&gt;
&lt;TD&gt;xystdat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;ab&lt;/TD&gt;
&lt;TD&gt;abdat_1&lt;/TD&gt;
&lt;TD&gt;abdat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;cd&lt;/TD&gt;
&lt;TD&gt;cddat_1&lt;/TD&gt;
&lt;TD&gt;cddat&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;call execute('data test;&lt;BR /&gt;set "||strip(libname)||"."||strip(memname)||";&lt;BR /&gt;%DT(DAT=&amp;amp;x);..........&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are 100s of datasets in memname and I don't know dates in each of those datasets. I am trying to identify date variable from the dataset and use those in call execute&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now I am even more confused.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you have a dataset that is the set of parameter values to pass to a macro.&lt;/P&gt;
&lt;P&gt;Let's assume you have a macro that takes as input the name of a dataset and the name of a variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%dt(ds=sashelp.class,var=age)
%dt(ds=sashelp.cars,var=hp)
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you have dataset with a list of dataset names and the corresponding variable names.&lt;/P&gt;
&lt;P&gt;Just use the dataset to generate the macro calls so that they run AFTER the data step that generated them ends.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call execute(catx(' '
    ,'%nrstr(%dt)'
    ,'(ds=',catx('.',libname,memname)
    ,',var=',varname
    ')'
  ));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 22 Apr 2022 23:58:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-04-22T23:58:42Z</dc:date>
    <item>
      <title>Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809395#M319187</link>
      <description>&lt;P&gt;Dummy Data:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MEMNAME&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;_var1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var3&lt;/TD&gt;&lt;TD&gt;var4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;XY&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XYSTDAT_1&lt;/TD&gt;&lt;TD&gt;XYENDAT_2&lt;/TD&gt;&lt;TD&gt;XYXYDDAT_3&lt;/TD&gt;&lt;TD&gt;XYXYDAT_4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I am looking for 2 different out in 2 different macro variables within a datastep:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1st macro variable should have&amp;nbsp;XYSTDAT&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2nd macro variable should have TEST_XYSTDAT&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I assign value from this "&lt;SPAN&gt;strip(reverse(substr(strip(reverse(testcase(i))),3)));" to a macro variable?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1) %let x =&amp;nbsp;&lt;SPAN&gt;strip(reverse(substr(strip(reverse(testcase(i))),3))); ------&amp;gt; will not work&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2) b =&amp;nbsp;strip(reverse(substr(strip(reverse(testcase(i))),3)));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let y = b; ----------------------&amp;gt; is not resolving either&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3) %let z = TEST_&amp;amp;x.&amp;nbsp; ----&amp;gt; Z resolves to TEST_strip(reverse(substr(strip(reverse(testcase(i))),3)))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a solution without using Call Symput (I get desired value with this), because I want to use the macro variable within the same datastep as the loop iterates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate your input! Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2022 23:13:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809395#M319187</guid>
      <dc:creator>Rain_28</dc:creator>
      <dc:date>2022-04-22T23:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809398#M319189</link>
      <description>&lt;P&gt;The request does not make much sense.&lt;/P&gt;
&lt;P&gt;What do you want to do with the macro variable during data step that you could not do with a REAL variable?&lt;/P&gt;
&lt;P&gt;If you need the macro variable to generate SAS code then by definition the macro variable has to exist before SAS begins compiling the data step.&amp;nbsp; &amp;nbsp;So it cannot use any value read during the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is TESTCASE?&amp;nbsp; That does not appear in your example data.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2022 23:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809398#M319189</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-22T23:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809399#M319190</link>
      <description>&lt;P&gt;Memname is actually a dataset name and dates is actual date variable (that I am extracting from _var1).&lt;/P&gt;&lt;P&gt;want to put Dates in macro variable and call datasets with call execute. As the loop iterates want to pick memname and correspondaing dates variable from that memname.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MEMNAME&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;_var1&lt;/TD&gt;&lt;TD&gt;dates&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;XY&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XYSTDAT_1&lt;/TD&gt;&lt;TD&gt;xystdat&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ab&lt;/TD&gt;&lt;TD&gt;abdat_1&lt;/TD&gt;&lt;TD&gt;abdat&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;cd&lt;/TD&gt;&lt;TD&gt;cddat_1&lt;/TD&gt;&lt;TD&gt;cddat&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;call execute('data test;&lt;BR /&gt;set "||strip(libname)||"."||strip(memname)||";&lt;BR /&gt;%DT(DAT=&amp;amp;x);..........&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are 100s of datasets in memname and I don't know dates in each of those datasets. I am trying to identify date variable from the dataset and use those in call execute&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2022 23:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809399#M319190</guid>
      <dc:creator>Rain_28</dc:creator>
      <dc:date>2022-04-22T23:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809402#M319191</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/241108"&gt;@Rain_28&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Memname is actually a dataset name and dates is actual date variable (that I am extracting from _var1).&lt;/P&gt;
&lt;P&gt;want to put Dates in macro variable and call datasets with call execute. As the loop iterates want to pick memname and correspondaing dates variable from that memname.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;MEMNAME&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;_var1&lt;/TD&gt;
&lt;TD&gt;dates&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;XY&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XYSTDAT_1&lt;/TD&gt;
&lt;TD&gt;xystdat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;ab&lt;/TD&gt;
&lt;TD&gt;abdat_1&lt;/TD&gt;
&lt;TD&gt;abdat&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;cd&lt;/TD&gt;
&lt;TD&gt;cddat_1&lt;/TD&gt;
&lt;TD&gt;cddat&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;call execute('data test;&lt;BR /&gt;set "||strip(libname)||"."||strip(memname)||";&lt;BR /&gt;%DT(DAT=&amp;amp;x);..........&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are 100s of datasets in memname and I don't know dates in each of those datasets. I am trying to identify date variable from the dataset and use those in call execute&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now I am even more confused.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you have a dataset that is the set of parameter values to pass to a macro.&lt;/P&gt;
&lt;P&gt;Let's assume you have a macro that takes as input the name of a dataset and the name of a variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%dt(ds=sashelp.class,var=age)
%dt(ds=sashelp.cars,var=hp)
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you have dataset with a list of dataset names and the corresponding variable names.&lt;/P&gt;
&lt;P&gt;Just use the dataset to generate the macro calls so that they run AFTER the data step that generated them ends.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call execute(catx(' '
    ,'%nrstr(%dt)'
    ,'(ds=',catx('.',libname,memname)
    ,',var=',varname
    ')'
  ));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Apr 2022 23:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809402#M319191</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-22T23:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809422#M319206</link>
      <description>&lt;P&gt;To keep values across data step iterations, use retained variables (or a temporary array, which is also retained).&lt;/P&gt;
&lt;P&gt;To dynamically set and fetch macro variables, use CALL SYMPUT and SYMGET.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Apr 2022 07:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/809422#M319206</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-23T07:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/810215#M319502</link>
      <description>&lt;P&gt;Yes, the code worked by writing the whole datastep in call execute...this is the first time I wrote complicated call execute within multiple loops in a macro. It took me couple of days but got it....Thank you so much! Really appreciate your help.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 16:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/810215#M319502</guid>
      <dc:creator>Rain_28</dc:creator>
      <dc:date>2022-04-27T16:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value to a macro variable within a datastep (without call symput)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/810223#M319504</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/241108"&gt;@Rain_28&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes, the code worked by writing the whole datastep in call execute...this is the first time I wrote complicated call execute within multiple loops in a macro. It took me couple of days but got it....Thank you so much! Really appreciate your help.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Good.&amp;nbsp; The point of making a macro that the code generation can call is to simplify the data driven code generation step.&amp;nbsp; Rather than generating the full data step code you just need to pass the parts that vary to a macro.&amp;nbsp; Then you can debug the process of converting the parts that vary into proper code independent from the data driven step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also skip CALL EXECUTE() and just use the data step to write the code directly to a file.&amp;nbsp; This will normally be a lot easier to debug.&amp;nbsp; Plus you can use the power of the PUT statement and not have to fight with&amp;nbsp; text manipulation function calls.&amp;nbsp; You can examine the file and make sure it will work. Copy and paste part of the code back into the editor window and run it to see if it is going to work. etc.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set metadata ;
  file code;
  put 'data new;'
    / '  set ' dsname ';'
    / 'run;'
  ;
run;
%include code / source2;
  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Apr 2022 16:51:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-to-a-macro-variable-within-a-datastep-without/m-p/810223#M319504</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-27T16:51:58Z</dc:date>
    </item>
  </channel>
</rss>

