<?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: apply formula that in data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954188#M372697</link>
    <description>&lt;P&gt;I think the difficulty with this question is that you've actually created an over-simplified example of what you actually need.&amp;nbsp; Here, you've got a formula table that you're joining (in a Cartesian way) to your data, but the formula table only, in this example, contains a single formula, so it's not clear how the join would be modified if it contained multiple formulas that would be used conditionally based on, I guess, the data itself.&amp;nbsp; It would be easier for people to respond if your example formula table contained at least 2 different formulas and that the data step that followed showed us how you would use those formulas.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, I'm guessing you have a programming background that is maybe something other than SAS - nothing wrong with that - just saying that the way you're approaching this is very unusual and probably much more complicated than it needs to be.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With your current example, you could just skip the formula table (and the join) entirely and just run a macro, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro xy;
    x * y - 1
%mend;

data want;
set have;  
calc=%xy;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...but again, if you provide a more realistic example, we might be able to give you a better answer.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Dec 2024 19:01:54 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2024-12-19T19:01:54Z</dc:date>
    <item>
      <title>apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953802#M372582</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's say that&amp;nbsp; there is a data set with required formula to apply.&lt;/P&gt;
&lt;P&gt;What is the way to appy the formula that in the data set on other data set?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input x y z;
cards;
10 20 30
15 20 200
10 30 40
;
run;

data formula_tbl;
input formula $;
cards;
x+y-0.1*x
;
run;

proc sql;
create table t1 as
select *
from have,formula_tbl
;
quit;

data want;
set t1;
calc=formula;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 07:57:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953802#M372582</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-12-17T07:57:27Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953805#M372584</link>
      <description>&lt;P&gt;The goto method of generating dynamic logic in SAS is by using macros, and that should work fine for this simple scenario.&lt;/P&gt;
&lt;P&gt;So read your data set formula and push the formula to a macro variable by using CALL SYMPUT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My guess is that your real life scenario is slightly more complicated/extensive, so I need to see more to determine if CALL SYMPUT would work.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 08:04:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953805#M372584</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-12-17T08:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953807#M372585</link>
      <description>&lt;P&gt;A common method involves generating the SAS code, writing it to an external file, and then including this file in the subsequent data step for execution.&lt;/P&gt;
&lt;P&gt;Although the data _null_ step with the put statement and the necessary quoting can become somewhat cumbersome, I appreciate this approach because the include statement enables you to write the generated code to the SAS log exactly where it is executed. This makes troubleshooting and debugging significantly easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
	input x y z;
	cards;
10 20 30
15 20 200
10 30 40
;
run;

data formula_tbl;
	infile datalines dsd dlm='~';
	input target_var :$32. expression :$100.;
	cards;
calc~x+y-0.1*x
;
run;

filename codegen temp;
data _null_;
/* 	file print; */
	file codegen;
	set formula_tbl;
	put target_var '=' expression ';';
run;

data want;
	set have;
	%include codegen /source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1734425270510.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103072i050BD48AA1E61B43/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1734425270510.png" alt="Patrick_0-1734425270510.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 08:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953807#M372585</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-12-17T08:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953809#M372586</link>
      <description>There is an blog article from Yinliang Wu: &lt;A href="https://blogs.sas.com/content/sgf/2021/06/25/how-to-evaluate-sas-expression-in-data-step-dynamically/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2021/06/25/how-to-evaluate-sas-expression-in-data-step-dynamically/&lt;/A&gt;</description>
      <pubDate>Tue, 17 Dec 2024 08:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953809#M372586</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-12-17T08:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953832#M372591</link>
      <description>&lt;P&gt;This might help:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Polish-SAS-Users-Group/Kolejny-pow%C3%B3d-by-pokocha%C4%87-hash-tablice/m-p/640297#M22" target="_blank"&gt;https://communities.sas.com/t5/Polish-SAS-Users-Group/Kolejny-pow%C3%B3d-by-pokocha%C4%87-hash-tablice/m-p/640297#M22&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The comment is in Polish but I'm sure google translator will help you. And the code is in SAS so you should be able to understand it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 12:09:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953832#M372591</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-12-17T12:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953833#M372592</link>
      <description>&lt;P&gt;code for your case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input x y z;
cards;
10 20 30
15 20 200
10 30 40
;
run;

data formula_tbl;
input formula $ 12.;
cards;
x+y-0.1*x
;
run;


options dlcreatedir;
libname x "%sysfunc(pathname(work))\spde\";
libname x SPDE "%sysfunc(pathname(work))\spde\";

proc sql;
create table x.testc as
select have.*, formula as CODE
from have,formula_tbl
;
quit;






/* options nonotes; */ /* odkomentuj zeby miec czysty log */
data testc3;
  set 
    x.testc 
  curobs=curobs indsname=indsname end=end
  ;

  if missing(code) then 
    do;
      output;
    end;
  else
    do;
      length strX $ 1024;
      strX = cat(
        strip(indsname)
        , '(startobs = ', curobs  /* &amp;lt;-- */
        , '   endobs = ', curobs  /* &amp;lt;-- */
        , '    where = (', strip(code), '))' 
        );
      declare hash H(dataset:strX);
      rc = H.defineKey("code");
      rc = H.defineDone();
      if H.num_items &amp;gt; 0 then output;
      rc = H.delete();
    end;

  if end then put _N_=;
run;
/*options notes;*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 12:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953833#M372592</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-12-17T12:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953886#M372607</link>
      <description>Is there limit of length thst macro var can store?</description>
      <pubDate>Tue, 17 Dec 2024 18:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953886#M372607</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-12-17T18:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953894#M372608</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Is there limit of length thst macro var can store?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes. 64K Bytes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is another reason why using a data step to write the code to a file is a better solution for code generation problems like this.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 18:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953894#M372608</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-17T18:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953947#M372624</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input x y z;
cards;
10 20 30
15 20 200
10 30 40
;
run;

data formula_tbl;
input formula $20.;
cards;
x+y-0.1*x
x+y+z
;
run;

proc sql;
create table t1 as
select *
from have,formula_tbl
;
quit;

data want;
set t1;
length _formula $ 200;
_formula=formula;
array _x{*} _numeric_;
do i=1 to dim(_x);
 _formula=tranwrd(lowcase(_formula),lowcase(vname(_x{i})),strip(_x{i}));
end;
result=input(resolve(cats('%sysevalf(',_formula,')')),best32.);
drop i _formula;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2024 03:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/953947#M372624</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-18T03:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954008#M372649</link>
      <description>&lt;P&gt;One more approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table testc as
select have.*, formula as CODE
from have,formula_tbl
;
quit;

proc sql;
  create table code_b as 
  select distinct QUOTE(strip(code)) as q, count(code) as cc
  from testc
  order by cc desc ;
  ; 
quit;

filename testb2 TEMP lrecl=2000;

data _null_;
  file testb2;
  length _X_ $ 2000;

  put "data testb2;";
  put "set testc;";
  put "select;";
  do until(EOF);
    set code_b end=EOF;
    _X_ = "when (code = " 
        || strip(q)
        || ") do; result = (" 
        || dequote(q)
        || "); end;"; 
    put _X_;
  end;
  put "otherwise;";
  put "end;";
  put "run;";
stop;
run;

%include testb2 / SOURCE2;
filename testb2;
proc print data=testb2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2024 15:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954008#M372649</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-12-18T15:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954120#M372690</link>
      <description>&lt;P&gt;One more approach:&lt;/P&gt;
&lt;P&gt;Especially for the formula which is unable to handle by %sysevalf().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input x y z;
cards;
10 20 30
15 20 200
10 30 40
;
run;

data formula_tbl;
input formula $20.;
cards;
x+y-0.1*x
x+y+z
;
run;

proc sql;
create table t1 as
select *
from have,formula_tbl
;
quit;

filename x temp;
data _null_;
 set t1 end=last;
 file x;
 if _n_=1 then put 'data want; set t1;';
 put 'if _n_=' _n_ 'then result=' formula ';';
 if last then put 'run;';
run;
%include x;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2024 01:37:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954120#M372690</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-19T01:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954175#M372695</link>
      <description>&lt;P&gt;Don't use STRIP if the intent is to generate a quoted string that represent the actual value of a variable.&amp;nbsp; It will remove the leading spaces so the resulting strings are NOT the same.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make the quoted string shorter just use TRIM() as trailing spaces are not significant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  code = '   xxx    ';
run;

proc sql noprint;
  select quote(trim(code),"'")
       , quote(strip(code),"'") 
    into :good trimmed
       , :bad trimmed
  from have
  ;
quit;

data test;
  set have;
  if code = &amp;amp;good then put "GOOD string &amp;amp;good matched " code= :$quote.;
  else  put "GOOD string &amp;amp;good NOT matched " code= :$quote.;
  if code = &amp;amp;bad  then put "BAD string &amp;amp;bad matched " code= :$quote.;
  else put "BAD string &amp;amp;bad NOT matched " code= :$quote.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;295  data test;
296    set have;
297    if code = &amp;amp;good then put "GOOD string &amp;amp;good matched " code= :$quote.;
298    else  put "GOOD string &amp;amp;good NOT matched " code= :$quote.;
299    if code = &amp;amp;bad  then put "BAD string &amp;amp;bad matched " code= :$quote.;
300    else put "BAD string &amp;amp;bad NOT matched " code= :$quote.;
301  run;

GOOD string '   xxx' matched code="   xxx"
BAD string 'xxx' NOT matched code="   xxx"
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2024 16:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954175#M372695</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-19T16:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: apply formula that in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954188#M372697</link>
      <description>&lt;P&gt;I think the difficulty with this question is that you've actually created an over-simplified example of what you actually need.&amp;nbsp; Here, you've got a formula table that you're joining (in a Cartesian way) to your data, but the formula table only, in this example, contains a single formula, so it's not clear how the join would be modified if it contained multiple formulas that would be used conditionally based on, I guess, the data itself.&amp;nbsp; It would be easier for people to respond if your example formula table contained at least 2 different formulas and that the data step that followed showed us how you would use those formulas.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, I'm guessing you have a programming background that is maybe something other than SAS - nothing wrong with that - just saying that the way you're approaching this is very unusual and probably much more complicated than it needs to be.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With your current example, you could just skip the formula table (and the join) entirely and just run a macro, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro xy;
    x * y - 1
%mend;

data want;
set have;  
calc=%xy;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...but again, if you provide a more realistic example, we might be able to give you a better answer.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2024 19:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/apply-formula-that-in-data-set/m-p/954188#M372697</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2024-12-19T19:01:54Z</dc:date>
    </item>
  </channel>
</rss>

