<?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: timing of SYMEXIST function within data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187558#M35503</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;On second thought, CALL EXECUTE will solve the first problem, but not the second.&amp;nbsp; It will create &amp;amp;MACVAR1 only if needed.&amp;nbsp; But it won't create it in time for the final DATA step IF/THEN (TEST 3) to find it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It would be reasonable to just move the %GLOBAL statement to before the DATA step begins.&amp;nbsp; It won't do anything if &amp;amp;MACVAR1 already exists.&amp;nbsp; Then the DATA step can always resolve &amp;amp;MACVAR1&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 03 Jan 2014 15:37:57 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2014-01-03T15:37:57Z</dc:date>
    <item>
      <title>timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187555#M35500</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm trying to set something up within a DATA _NULL_ step to test whether or not a global macro variable exists, and then if it doesn't to create it and assign a value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, it looks like maybe my %GLOBAL statement is getting resolved before the DATA step finishes executing?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's some example code I came up with to demonstrate:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 1" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;if symexist("MacVar1") then do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "TEST INSIDE DO LOOP 1" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;else do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "TEST INSIDE DO LOOP 2" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "MacVar1 macro variable does not yet exist" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %global MacVar1; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 2" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;if "&amp;amp;MacVar1"="" then call symputx ('MacVar1','0') ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 3" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 4" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;if symexist("MacVar2") then do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "TEST INSIDE DO LOOP 3" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;else do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "TEST INSIDE DO LOOP 4" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "MacVar2 macro variable does not yet exist" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 5" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;if "&amp;amp;MacVar2"="" then call symputx ('MacVar2','0') ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 6" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The only functional difference between those two DATA steps is the "%GLOBAL" statement within the ELSE DO. When I run it, I get the following in the log:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST INSIDE DO LOOP 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;NOTE: DATA statement used (Total process time):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;WARNING: Apparent symbolic reference MACVAR2 not resolved.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST 4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST INSIDE DO LOOP 4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;MacVar2 macro variable does not yet exist&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST 5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;TEST 6&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;NOTE: DATA statement used (Total process time):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.00 seconds&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.00 seconds&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In other words, in the first DATA step (the one with the %GLOBAL statement), it detects the existence of MacVar1, even though the %GLOBAL statement is &lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;after&lt;/EM&gt;&lt;/SPAN&gt; the SYMEXIST.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, can anyone help me figure out the right way to set this up? Basically, I would like to check whether or not MacVar1 exists. If it doesn't exist, then I'd like to create it as a global macro variable. Either way, does or does not exist, I then want to check whether or not it has a value, and if it doesn't then assign a value to it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 15:21:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187555#M35500</guid>
      <dc:creator>gfarkas</dc:creator>
      <dc:date>2014-01-03T15:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187556#M35501</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;use SYMPUTX with global option in place of %GLOBAL.&amp;nbsp; You are correct %GLOBAL has already happened before data step starts.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 15:31:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187556#M35501</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2014-01-03T15:31:26Z</dc:date>
    </item>
    <item>
      <title>Re: timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187557#M35502</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As you have seen, a DATA step cannot use IF/THEN to determine whether to execute a %GLOBAL statement.&amp;nbsp; The %GLOBAL statement is not part of the DATA step, and executes immediately (before SAS finishes interpreting the meaning of the DATA step statements).&amp;nbsp; While there are other approaches to solving your original problem, the simplest answer to your question is to change the %GLOBAL statement to this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;call execute('%global macvar1;');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 15:31:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187557#M35502</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-01-03T15:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187558#M35503</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;On second thought, CALL EXECUTE will solve the first problem, but not the second.&amp;nbsp; It will create &amp;amp;MACVAR1 only if needed.&amp;nbsp; But it won't create it in time for the final DATA step IF/THEN (TEST 3) to find it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It would be reasonable to just move the %GLOBAL statement to before the DATA step begins.&amp;nbsp; It won't do anything if &amp;amp;MACVAR1 already exists.&amp;nbsp; Then the DATA step can always resolve &amp;amp;MACVAR1&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 15:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187558#M35503</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-01-03T15:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187559#M35504</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks! That definitely makes sense. I know the %GLOBAL wasn't actually part of the DATA step, so I don't know why I was thinking about it that way before. :smileyshocked:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I ended up going with:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 1" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;if ^symexist("MacVar1") then do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "TEST INSIDE DO LOOP 2" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "MacVar1 macro variable does not yet exist" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; call execute('%global MacVar1;');&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 2" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;if "&amp;amp;MacVar1"="" then call symputx ('MacVar1','0') ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;put "TEST 3" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;That seems to give me what I'm looking for. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Thanks again!&lt;/SPAN&gt; &lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 15:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187559#M35504</guid>
      <dc:creator>gfarkas</dc:creator>
      <dc:date>2014-01-03T15:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187560#M35505</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe you are making this more complicated than you need&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is the action any different if MACVAR1 does or does not exit.&amp;nbsp; In other words do you always want it GLOBAL and to give it a new value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why not just use SYMPUTX('MACVAR1',0,'GLOBAL');&amp;nbsp; ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 16:25:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187560#M35505</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2014-01-03T16:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: timing of SYMEXIST function within data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187561#M35506</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Once you start examining alternatives (rather than solving for the original error message), you might get rid of the DATA step entirely:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%global macvar1;&lt;/P&gt;&lt;P&gt;%if %length(&amp;amp;macvar1)=0 %then %let macvar1=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are possible complications if your statements appear inside a macro that might have a local macro variable named macvar1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Jan 2014 17:35:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/timing-of-SYMEXIST-function-within-data-step/m-p/187561#M35506</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-01-03T17:35:25Z</dc:date>
    </item>
  </channel>
</rss>

