<?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: resolving a formula in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68124#M14768</link>
    <description>Your variable Formula is only a character variable.&lt;BR /&gt;
&lt;BR /&gt;
This means for the first observation in your second dataset is equivalent to &lt;BR /&gt;
&lt;BR /&gt;
result="2*var1";&lt;BR /&gt;
&lt;BR /&gt;
which is not the same as&lt;BR /&gt;
&lt;BR /&gt;
result=2*var1;&lt;BR /&gt;
&lt;BR /&gt;
Only macro variables resolve the way that you are thinking.  You should take another look at what you are trying to accomplish and try to figure out a simpler solution.</description>
    <pubDate>Tue, 01 Feb 2011 20:04:33 GMT</pubDate>
    <dc:creator>RickM</dc:creator>
    <dc:date>2011-02-01T20:04:33Z</dc:date>
    <item>
      <title>resolving a formula in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68123#M14767</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a table with 2 columns. One is simple formula and the other one a numeric variable. I am trying to resolve the formula in the dataset but it does not work and I am not sure to understand why. Below is the example:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
DATA temp1; &lt;BR /&gt;
   INPUT formula $ var1 ; &lt;BR /&gt;
   DATALINES; &lt;BR /&gt;
 2*var1 1&lt;BR /&gt;
 23*var1 2&lt;BR /&gt;
 12*var1 5&lt;BR /&gt;
; &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp2;&lt;BR /&gt;
set temp1;&lt;BR /&gt;
format result 8.;&lt;BR /&gt;
result=formula;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Why is it generating an error? if ifdo something like this, it is perfectly fine.&lt;BR /&gt;
 data temp2;&lt;BR /&gt;
set temp1;&lt;BR /&gt;
format result 8.;&lt;BR /&gt;
result=var1*5;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thnaks a lot</description>
      <pubDate>Tue, 01 Feb 2011 19:44:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68123#M14767</guid>
      <dc:creator>mrom34</dc:creator>
      <dc:date>2011-02-01T19:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: resolving a formula in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68124#M14768</link>
      <description>Your variable Formula is only a character variable.&lt;BR /&gt;
&lt;BR /&gt;
This means for the first observation in your second dataset is equivalent to &lt;BR /&gt;
&lt;BR /&gt;
result="2*var1";&lt;BR /&gt;
&lt;BR /&gt;
which is not the same as&lt;BR /&gt;
&lt;BR /&gt;
result=2*var1;&lt;BR /&gt;
&lt;BR /&gt;
Only macro variables resolve the way that you are thinking.  You should take another look at what you are trying to accomplish and try to figure out a simpler solution.</description>
      <pubDate>Tue, 01 Feb 2011 20:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68124#M14768</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2011-02-01T20:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: resolving a formula in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68125#M14769</link>
      <description>Assuming that you will always multiply with Var1.&lt;BR /&gt;
&lt;BR /&gt;
Strip the first value before the '*' in Formula and then use it to multiply with Var1.&lt;BR /&gt;
&lt;BR /&gt;
DATA temp1;&lt;BR /&gt;
	INPUT formula $ var1 ;&lt;BR /&gt;
	DATALINES;&lt;BR /&gt;
2*var1 1&lt;BR /&gt;
23*var1 2&lt;BR /&gt;
12*var1 5&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data temp2;&lt;BR /&gt;
	set temp1;&lt;BR /&gt;
	format result 8.;&lt;BR /&gt;
	*result=formula;&lt;BR /&gt;
	result=input(scan(formula,1,'*'),best.) * Var1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=temp2;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Obs    formula    var1      result&lt;BR /&gt;
&lt;BR /&gt;
   1     2*var1       1            2&lt;BR /&gt;
   2     23*var1      2           46&lt;BR /&gt;
   3     12*var1      5           60&lt;BR /&gt;
&lt;BR /&gt;
When you assign Formula to Result, you are merely copying over the value to Result.&lt;BR /&gt;
I don't think you can resolve a formula in a datastep.

Message was edited by: bhavani</description>
      <pubDate>Wed, 02 Feb 2011 06:01:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68125#M14769</guid>
      <dc:creator>bhavani</dc:creator>
      <dc:date>2011-02-02T06:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: resolving a formula in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68126#M14770</link>
      <description>See this posting from a couple of months ago.  Chang showed a solution here that proves it is indeed possible to resolve a formula in a datastep.&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://support.sas.com/forums/thread.jspa?messageID=47101럽" target="_blank"&gt;http://support.sas.com/forums/thread.jspa?messageID=47101럽&lt;/A&gt;</description>
      <pubDate>Wed, 02 Feb 2011 15:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68126#M14770</guid>
      <dc:creator>polingjw</dc:creator>
      <dc:date>2011-02-02T15:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: resolving a formula in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68127#M14771</link>
      <description>&lt;P&gt;Hi.&lt;BR /&gt; If you have very large dataset, use the &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0pgemqcslm9uen1tvr5gcrusgrw.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;tranwrd function&lt;/A&gt; and &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1hffi0qkqzu78n1quvvl1szy9k6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;resolve function&lt;/A&gt;&amp;nbsp;with %sysevalf&amp;nbsp;:&lt;BR /&gt; &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA temp1;
INPUT formula $ var1 ;
DATALINES;
2*var1 1
23*var1 2
12*var1 5
; 
data result;
 set temp1;
 temp=tranwrd(formula ,'var1',strip(var1));
 result=resolve('%sysevalf('||temp||')');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; &lt;BR /&gt; If you have not very large dataset, CALL EXECUTE can generate the statements for each record to calculate the result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA temp1;
INPUT formula $ var1 ;
DATALINES;
2*var1 1
23*var1 2
12*var1 5
; 

data _null_;
 set temp1 end=last;
 if _n_=1 then call execute('data _result;');
 call execute('var1='||var1||';');
 call execute('formula="'||formula||'";');
 call execute('result='||formula||';');
 call execute('output;');
 if last then call execute('run;');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; Ksharp&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 19:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/m-p/68127#M14771</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-11-15T19:46:25Z</dc:date>
    </item>
  </channel>
</rss>

