<?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 Run SAS code found in a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789404#M252600</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a SAS dataset with two variables, STATE and FORMULA that looks like this. The values of FORMULA are if/then statements with semicolons.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Arizona&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if 2&amp;gt;1 then BrandNewVariable = "YES";&lt;BR /&gt;Texas&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 2&amp;gt;3 then BrandNewVariable = "YES";&lt;BR /&gt;California&amp;nbsp; &amp;nbsp; &amp;nbsp; if 3&amp;gt;0 then BrandNewVariable = "YES";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to use the FORMULA variable to within a data step (or any other method) to get a data set with three variables, STATE, FORMULA, BRANDNEWVARIABLE?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Arizona&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if 2&amp;gt;1 then BrandNewVariable = "YES";&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YES&lt;BR /&gt;Texas&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 2&amp;gt;3 then BrandNewVariable = "YES";&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;California&amp;nbsp; &amp;nbsp; &amp;nbsp; if 3&amp;gt;0 then BrandNewVariable = "YES";&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YES&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jan 2022 06:28:57 GMT</pubDate>
    <dc:creator>the_sheriff</dc:creator>
    <dc:date>2022-01-11T06:28:57Z</dc:date>
    <item>
      <title>Run SAS code found in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789404#M252600</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a SAS dataset with two variables, STATE and FORMULA that looks like this. The values of FORMULA are if/then statements with semicolons.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Arizona&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if 2&amp;gt;1 then BrandNewVariable = "YES";&lt;BR /&gt;Texas&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 2&amp;gt;3 then BrandNewVariable = "YES";&lt;BR /&gt;California&amp;nbsp; &amp;nbsp; &amp;nbsp; if 3&amp;gt;0 then BrandNewVariable = "YES";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to use the FORMULA variable to within a data step (or any other method) to get a data set with three variables, STATE, FORMULA, BRANDNEWVARIABLE?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Arizona&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if 2&amp;gt;1 then BrandNewVariable = "YES";&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YES&lt;BR /&gt;Texas&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 2&amp;gt;3 then BrandNewVariable = "YES";&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;California&amp;nbsp; &amp;nbsp; &amp;nbsp; if 3&amp;gt;0 then BrandNewVariable = "YES";&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YES&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jan 2022 06:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789404#M252600</guid>
      <dc:creator>the_sheriff</dc:creator>
      <dc:date>2022-01-11T06:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: Run SAS code found in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789410#M252605</link>
      <description>&lt;P&gt;Not clear what you expect as result. &lt;/P&gt;
&lt;P&gt;A data null step could be used to create another data step, either by using call execute or by writing sas statements to a file that can be included/executed.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jan 2022 07:56:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789410#M252605</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-01-11T07:56:30Z</dc:date>
    </item>
    <item>
      <title>Re: Run SAS code found in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789421#M252612</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are several ways to do this.&lt;/P&gt;
&lt;P&gt;Either way you choose, you'll need to dynamically generate the code you would like to run.&lt;/P&gt;
&lt;P&gt;You can either choose to directly run the generated code or outsource it to a macro or even outsource it to a new sas file, it's up to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For my part I think I would dynamically generate a macro code that I would compile and run on my data.&lt;/P&gt;
&lt;P&gt;It would look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
length STATE $20 FORMULA $200;
infile datalines TRUNCOVER;
input STATE $20. FORMULA $200.;
Datalines4;
Arizona             if 2&amp;gt;1 then BrandNewVariable = "YES";
Texas               if 2&amp;gt;3 then BrandNewVariable = "YES";
California          if 3&amp;gt;0 then BrandNewVariable = "YES";
;;;;
RUN;

data _NULL_;
   set have end=last;
   length macroCode $2000;
   retain macroCode '';
   if _N_ eq 1 then macroCode=cats('%MACRO runSTATECode(state=);');
   macroCode=cats(macroCode,'if strip(upcase(state)) eq "',upcase(STATE),'" then do;',formula,'end;');
   if last then do;
      macroCode=cats(macroCode,'%MEND runSTATECode;');
      rc=dosubl(macroCode);*compile macro code;
      put macroCode=;
   end;
run;

options mprint nomprintnest ;
data want;
   set have;/*or any other dataset with testdata*/
   LENGTH BrandNewVariable $3;
   %runSTATECode();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jan 2022 09:53:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789421#M252612</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-01-11T09:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: Run SAS code found in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789423#M252614</link>
      <description>&lt;P&gt;If the name of a newly created variable in formula is arbitrary, then you cannot solve this in one step, as the calling step must define all its variables before it starts processing.&lt;/P&gt;
&lt;P&gt;You need to run a separate data step for each observation in the source dataset, and concatenate all resulting datasets in the end:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="|";
input state :$20. formula :$50.;
datalines4;
Arizona|if 2&amp;gt;1 then BrandNewVariable = "YES";
Texas|if 2&amp;gt;3 then BrandNewVariable = "YES";
California|if 3&amp;gt;0 then BrandNewVariable = "YES";
;;;;

data _null_;
set have end=done;
call execute(cats(
  "data ds",
  put(_n_,z10.),
  ";length state $20;state='",
  state,
  "';",
  formula,
  "run;"
));
if done
then do;
  call execute("data want;set");
  do i = 1 to _n_;
    call execute(" ds"!!put(i,z10.));
  end;
  call execute(";run;");
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jan 2022 10:22:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789423#M252614</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-11T10:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: Run SAS code found in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789437#M252619</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input state  : $40. formula $100.;
cards4;
Arizona        if 2&amp;gt;1 then BrandNewVariable = "YES";
Texas            if 2&amp;gt;3 then BrandNewVariable = "YES";
California      if 3&amp;gt;0 then BrandNewVariable = "YES";
;;;;

filename x temp;
data _null_;
set have end=last;
file x lrecl=200 ;
if _n_=1 then put 'data want;set have;';
put 'if _n_=' _n_ 'then do;' formula $100. 'end;';
if last then put 'run;';
run;

%include x /source source2 lrecl=200;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jan 2022 11:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789437#M252619</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-11T11:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: Run SAS code found in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789487#M252633</link>
      <description>&lt;P&gt;Hi guys&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the have dataset should be considered as lookup table where the formula for each state is saved.&lt;/P&gt;
&lt;P&gt;Therefore you're code will not produce the correct results when running on another dataset like&amp;nbsp;sashelp.prdsal2 for example.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jan 2022 15:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-SAS-code-found-in-a-variable/m-p/789487#M252633</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-01-11T15:54:22Z</dc:date>
    </item>
  </channel>
</rss>

