<?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: Update macro symbol table inside data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490451#M128311</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171227"&gt;@PJB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you ChrisNZ, this seems to work for what I would like to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To provide some additional detail: I have a dataset that contains a column with variable names and a column with variable lengths, and I would like to use this to generate dynamic length statements, preferably in a single macro call (so no prior initialization required). This is why it is of no use to retrieve values into data step variables (they basically already are, and these cannot be used in length statements) or to include another data step boundary.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's a helpful description.&amp;nbsp; This sounds to me like you want a function-style macro.&amp;nbsp; Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;'s test data and code, would you want a macro that reads the LENGTHS datasets and then generate the list of values for the length statement, e.g. :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  length %MakeLengthList(data=lengths)  ;  
  set sashelp.class ;
  newvar='hi' ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The cool thing about DOSUBL is that it can create such function-style macros that return code.&amp;nbsp; And the macros can run proc steps.&amp;nbsp; You need to use %SYSFUNC to call DOSUBL, so that the DOSUBL executes when the macro runs (before the data step has been compiled).&amp;nbsp; The macro looks like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MakeLengthList(data=);
  %local LengthList rc;

  %let rc=%sysfunc(dosubl(%nrstr(

    proc sql noprint;
    select catx(' ',var,'$',len) into :LengthList separated by ' '
    from &amp;amp;data;
    quit;

  )));

&amp;amp;LengthList /*return*/
%mend MakeLengthList;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lengths;
input var :$32. len;
cards;
name   20
sex     6
newvar  8
;

*see the list generated by the macro ;
%put %MakeLengthList(data=lengths) ;

data want ;
  length %MakeLengthList(data=lengths) ;
  set sashelp.class ;
  newvar='hi' ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Rick Langston wrote the original paper on using DOSUBL for this purpose.&amp;nbsp; See the last example in&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings13/032-2013.pdf.&amp;nbsp;&amp;nbsp;" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/032-2013.pdf.&amp;nbsp;&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Aug 2018 13:38:55 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2018-08-28T13:38:55Z</dc:date>
    <item>
      <title>Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/489872#M128007</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to update the macro symbol table inside a data step. Specifically, something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO TEST(name);
   rc = dosubl("
               data _null_;
                  set sashelp.class (where=(name eq '&amp;amp;name'));
                  call symputx('sex', sex,  'l');
               run;
         ");
   %put &amp;amp;sex;
%MEND;

data _null_;
   %TEST(Alfred);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;While running %TEST I would need any macro variable created by DOSUBL (or perhaps another method?) available in the (local) symbol table without halting the _null_ data step, I do not want this in a data step variable. From reading&amp;nbsp;&lt;A title="Problem Note 53059" href="http://support.sas.com/kb/53/059.html" target="_blank"&gt;http://support.sas.com/kb/53/059.html&lt;/A&gt; it appears that this may not be possible? Or is there another way?&lt;/P&gt;</description>
      <pubDate>Sun, 26 Aug 2018 07:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/489872#M128007</guid>
      <dc:creator>PJB</dc:creator>
      <dc:date>2018-08-26T07:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/489958#M128059</link>
      <description>&lt;P&gt;By default it would be loaded to the local table, but as soon as the macro ends, the local table is gone so not sure how that's useful.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171227"&gt;@PJB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to update the macro symbol table inside a data step. Specifically, something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO TEST(name);
   rc = dosubl("
               data _null_;
                  set sashelp.class (where=(name eq '&amp;amp;name'));
                  call symputx('sex', sex,  'l');
               run;
         ");
   %put &amp;amp;sex;
%MEND;

data _null_;
   %TEST(Alfred);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While running %TEST I would need any macro variable created by DOSUBL (or perhaps another method?) available in the (local) symbol table without halting the _null_ data step, I do not want this in a data step variable. From reading&amp;nbsp;&lt;A title="Problem Note 53059" href="http://support.sas.com/kb/53/059.html" target="_blank"&gt;http://support.sas.com/kb/53/059.html&lt;/A&gt; it appears that this may not be possible? Or is there another way?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 02:14:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/489958#M128059</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-27T02:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/489976#M128066</link>
      <description>&lt;P&gt;The point would be to do other things that the %put to these macro variables to return dynamic data step statements. The problem is that the macro variables are *not* loaded into the local symbol table. If I execute the code I posted I get the following behaviour:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;In the first call &amp;amp;sex gives a 'reference not resolved' warning. When the data step finishes however the macro variable is written to the GLOBAL symbol table (although the call symputx was specifically to local). You can %put &amp;amp;sex in open code and you'll get "M".&lt;/LI&gt;&lt;LI&gt;All next calls will reference this global variable, which is no longer modified by the macro. Running %TEST(Carol) in subsequent data steps will always still return Alfred's "M".&lt;/LI&gt;&lt;LI&gt;If I make call symputx address the global symbol table the reference will remain to the value returned from the previous data step, but at least now it can be updated in the global symbol table. That is, if I then add %TEST(Carol) twice in a data step each, the first will return Alfred's "M", the second "F" (which was set by the first %TEST(Carol) call).&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would there be a way to have access to the value for &amp;amp;sex in the &lt;EM&gt;current&lt;/EM&gt; data step?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 06:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/489976#M128066</guid>
      <dc:creator>PJB</dc:creator>
      <dc:date>2018-08-27T06:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490157#M128161</link>
      <description>&lt;P&gt;Run your code with OPTION MPRINT to see what statements you are generating.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 16:26:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490157#M128161</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-27T16:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490162#M128165</link>
      <description>&lt;P&gt;Is this closer? You can also store it in a variable as shown, just to demonstrate that it's available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%MACRO TEST(name);
   rc = dosubl("
                  set sashelp.class (where=(name eq ""&amp;amp;name""));
                  call symputx('sex', sex,  'l');
         ");
   %put &amp;amp;sex;
%MEND;

options mprint;
data _null_;
  x=resolve('%TEST(Alfred)');
  y=symget('sex');
  put 'Y=' y;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Aug 2018 16:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490162#M128165</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-27T16:36:42Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490285#M128231</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier"&gt;dosubl()&lt;/FONT&gt; creates another SAS instance which&amp;nbsp;disappears after the call ends.&lt;/P&gt;
&lt;P&gt;Its legacy are &lt;STRONG&gt;global&lt;/STRONG&gt; macro variables, data sets, formats, maybe more I haven't really dug deep yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note how these can be used:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  * Create format and table T1;
  RC= dosubl("proc format;value $A2Z 'A'='Z';run;data T1;A='A';put '1-' A $a2z.;run;");
  * New format is usable with putc but not with put (needs to be there at compile time);
  A='A'; B=put(A,$a2z.); C=putc(A,'$a2z.'); put '2-' B= C=;
  * Table T1 is usable with open function;
  DSID=open('T1'); put DSID=; RC=close(DSID); 
  * Table T1 is usable with hash table;
  dcl hash T1(dataset:'T1');
  T1.definekey('A');
  T1.definedone();
  RC=T1.check();
  put RC=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1-Z&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2-B=A C=Z&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;DSID=1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;RC=0&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the local variable that you create in &lt;FONT face="courier new,courier"&gt;dosubl&lt;/FONT&gt; only lives for the duration of that &lt;FONT face="courier new,courier"&gt;dosubl&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This does what you want (if your need is not more complex):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro TEST(name);
   %local sex dsid rc;
   %let dsid=%sysfunc(open(SASHELP.CLASS(where=(NAME="&amp;amp;name"))));
   %if &amp;amp;dsid=0 %then %return;
   %put &amp;amp;=dsid ;
   %let rc=%sysfunc(fetch(&amp;amp;dsid));
   %let sex=%sysfunc(getvarc(&amp;amp;dsid,%sysfunc(varnum(&amp;amp;dsid,SEX))));
   %put &amp;amp;=name &amp;amp;=sex;
   %let rc=%sysfunc(close(&amp;amp;dsid));
%mend;

data _null_;
  set SASHELP.CLASS;
  call execute('%test('||NAME||');');
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;DSID=1 &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;NAME=Alfred SEX=M&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 23:45:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490285#M128231</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-27T23:45:19Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490295#M128235</link>
      <description>&lt;P&gt;As you've written it, the macro %TEST will complete execution before the DOSUBL statement has run.&amp;nbsp; Because the DOSUBL doesn't run until the DATA step executes.&amp;nbsp; Thus any macro variables created in the side session can only be returned to the main session global symbol table, because there is no local symbol table.&amp;nbsp; If you want to write to a local symbol table, the DOSUBL statement needs to execute inside the macro, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO TEST(name) ;
  %local sex ;

  data _null_ ;
    rc = dosubl("
                data _null_ ;
                   set sashelp.class (where=(name eq '&amp;amp;name')) ;
                   call symputx('sex', sex) ; 
                run ;
          ");
    sex=symget("sex") ;
    put sex= ;
  run ;
 
  %put _local_ ;
%MEND;

%TEST(Alfred)

%TEST(Mary)

%TEST(Nope)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the above code, the DOSUBL statement executes before the macro %TEST has completed execution.&amp;nbsp; Thus macro variable generated by in the DOSUBL session can be returned to the local symbol table of %TEST.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, the big picture of your question is not really clear.&amp;nbsp; It might help to provide more description regarding what you're trying to do (and why).&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 01:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490295#M128235</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-08-28T01:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490306#M128240</link>
      <description>&lt;P&gt;Can you explain a situation where there is some value in doing that?&lt;/P&gt;
&lt;P&gt;Also can you create an example that is actually running inside of a macro so that there is a local symbol table that the data step could access?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 02:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490306#M128240</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-28T02:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490307#M128241</link>
      <description>&lt;P&gt;You need a step delimiter between the RC=dosubl and the %put statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO TEST(name);
   rc = dosubl("
               data _null_;
                  set sashelp.class (where=(name eq '&amp;amp;name'));
                  call symputx('sex', sex,  'l');
               run;
         ");
   run;
   %put &amp;amp;=sex;
%MEND;

data _null_;
   %TEST(Alfred);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Apparently the run inside the dosubl is not enough for SAS to recognize that the %put statement has to wait for the previous code to execute.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;edited addition: Or does this suggestion defeat what you intended to do?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 02:55:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490307#M128241</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-28T02:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490311#M128245</link>
      <description>&lt;P&gt;If the macro generates a RUN statement then the calling program does not need a RUN statement.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 03:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490311#M128245</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-28T03:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490312#M128246</link>
      <description>&lt;P&gt;To weigh in on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;'s timing remark, here are attempts to output the value of the macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO TEST(name);
  rc=dosubl("data _null_;
               set sashelp.class(where=(name='&amp;amp;name'));
               call symputx('sex', sex, 'l');
               a= symget('sex');
               put '1-' A;
               call execute('%nrstr(%put 2-&amp;amp;sex;)');
               call execute('%put 3-%superq(sex);');
             run;
             %put 4-%superq(sex);");
  %put 5-%superq(sex);
  rc = dosubl('%put 6-&amp;amp;sex;');    
%MEND;

data _null_;
  %TEST(Alfred);
run;
%put 7-&amp;amp;sex;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3-&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;4-&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;5-&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1-M&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2-M&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;6-M&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;7-M&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see the macro code inside the macro (outputs 3, 4, 5) runs before anything else is executed.&lt;/P&gt;
&lt;P&gt;After &lt;FONT face="courier new,courier"&gt;symput&lt;/FONT&gt; has run, &lt;FONT face="courier new,courier"&gt;symget&lt;/FONT&gt; and the subsequent calls&amp;nbsp;are successful retrieving the value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the difference between 2 and 3 where we delay the execution of 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 03:30:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490312#M128246</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-28T03:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490352#M128275</link>
      <description>&lt;P&gt;Thank you ChrisNZ, this seems to work for what I would like to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To provide some additional detail: I have a dataset that contains a column with variable names and a column with variable lengths, and I would like to use this to generate dynamic length statements, preferably in a single macro call (so no prior initialization required). This is why it is of no use to retrieve values into data step variables (they basically already are, and these cannot be used in length statements) or to include another data step boundary.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 07:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490352#M128275</guid>
      <dc:creator>PJB</dc:creator>
      <dc:date>2018-08-28T07:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490369#M128280</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171227"&gt;@PJB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset that contains a column with variable names and a column with variable lengths, and I would like to use this to generate dynamic length statements, preferably in a single macro call (so no prior initialization required).&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Like this (assuming &lt;EM&gt;character&lt;/EM&gt; variables only)?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lengths;
input var :$32. len;
cards;
name   20
sex     6
newvar  8
;

proc sql noprint;
select catx(' ',var,'$',len) into :lenspecs separated by ' '
from lengths;
quit;

data want;
length &amp;amp;lenspecs;
set sashelp.class;
/* more code */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Aug 2018 08:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490369#M128280</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-28T08:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490439#M128307</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171227"&gt;@PJB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you ChrisNZ, this seems to work for what I would like to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To provide some additional detail: I have a dataset that contains a column with variable names and a column with variable lengths, and I would like to use this to generate dynamic length statements, preferably in a single macro call (so no prior initialization required). This is why it is of no use to retrieve values into data step variables (they basically already are, and these cannot be used in length statements) or to include another data step boundary.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a well know use case and there is no need to use DOSUBL() for that. You need to KNOW what LENGTH statements you want to generate BEFORE the data step starts. This is because SAS compiles data steps before it starts running them.&amp;nbsp; So just generate the code and then include it in the data step definition.&amp;nbsp; If the list of variables is short you can just use PROC SQL to generate the code into a macro variable. If it is long for a single macro variable (64k bytes) then generate the code into a file and %INCLUDE it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if your metadata has VARNAME, TYPE and LENGTH you could generate LENGTH statement using code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set variable_list ;
  put 'LENGTH ' varname @;
  if type='char' then put '$' @;
  put length ';' ;
run;
data want ;
  %include code / source2;
   ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 13:08:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490439#M128307</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-28T13:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490451#M128311</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171227"&gt;@PJB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you ChrisNZ, this seems to work for what I would like to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To provide some additional detail: I have a dataset that contains a column with variable names and a column with variable lengths, and I would like to use this to generate dynamic length statements, preferably in a single macro call (so no prior initialization required). This is why it is of no use to retrieve values into data step variables (they basically already are, and these cannot be used in length statements) or to include another data step boundary.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's a helpful description.&amp;nbsp; This sounds to me like you want a function-style macro.&amp;nbsp; Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;'s test data and code, would you want a macro that reads the LENGTHS datasets and then generate the list of values for the length statement, e.g. :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  length %MakeLengthList(data=lengths)  ;  
  set sashelp.class ;
  newvar='hi' ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The cool thing about DOSUBL is that it can create such function-style macros that return code.&amp;nbsp; And the macros can run proc steps.&amp;nbsp; You need to use %SYSFUNC to call DOSUBL, so that the DOSUBL executes when the macro runs (before the data step has been compiled).&amp;nbsp; The macro looks like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MakeLengthList(data=);
  %local LengthList rc;

  %let rc=%sysfunc(dosubl(%nrstr(

    proc sql noprint;
    select catx(' ',var,'$',len) into :LengthList separated by ' '
    from &amp;amp;data;
    quit;

  )));

&amp;amp;LengthList /*return*/
%mend MakeLengthList;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lengths;
input var :$32. len;
cards;
name   20
sex     6
newvar  8
;

*see the list generated by the macro ;
%put %MakeLengthList(data=lengths) ;

data want ;
  length %MakeLengthList(data=lengths) ;
  set sashelp.class ;
  newvar='hi' ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Rick Langston wrote the original paper on using DOSUBL for this purpose.&amp;nbsp; See the last example in&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings13/032-2013.pdf.&amp;nbsp;&amp;nbsp;" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/032-2013.pdf.&amp;nbsp;&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 13:38:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490451#M128311</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-08-28T13:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: Update macro symbol table inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490462#M128315</link>
      <description>&lt;P&gt;Thank you, this is the solution to the approach I was trying.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 14:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-macro-symbol-table-inside-data-step/m-p/490462#M128315</guid>
      <dc:creator>PJB</dc:creator>
      <dc:date>2018-08-28T14:04:27Z</dc:date>
    </item>
  </channel>
</rss>

