<?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 Expression in an observation in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21615#M4611</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a dataset that contains, let's say, a text variable 'Operation' that contains operations on other columns (i.e. each observation is a different operation). For example, if the first obs. of 'Operation' is A+B, I would like then to add the columns A and B. If the second obs is 2*(C+D), then I want to add coluns C and D and multiply by two, and so on. So what I need is a datastep line that looks like this: result=('the content of 'Operation'). I tried a Call Execute but I can't make it work. Is there an easy solution to that problem?&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help,&lt;BR /&gt;
&lt;BR /&gt;
Nancy</description>
    <pubDate>Sun, 24 Oct 2010 20:20:11 GMT</pubDate>
    <dc:creator>Nancy</dc:creator>
    <dc:date>2010-10-24T20:20:11Z</dc:date>
    <item>
      <title>Expression in an observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21615#M4611</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a dataset that contains, let's say, a text variable 'Operation' that contains operations on other columns (i.e. each observation is a different operation). For example, if the first obs. of 'Operation' is A+B, I would like then to add the columns A and B. If the second obs is 2*(C+D), then I want to add coluns C and D and multiply by two, and so on. So what I need is a datastep line that looks like this: result=('the content of 'Operation'). I tried a Call Execute but I can't make it work. Is there an easy solution to that problem?&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help,&lt;BR /&gt;
&lt;BR /&gt;
Nancy</description>
      <pubDate>Sun, 24 Oct 2010 20:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21615#M4611</guid>
      <dc:creator>Nancy</dc:creator>
      <dc:date>2010-10-24T20:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: Expression in an observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21616#M4612</link>
      <description>Hi Nancy&lt;BR /&gt;
&lt;BR /&gt;
An approach like in the code below could work for you.&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  infile datalines dsd dlm=';' truncover;&lt;BR /&gt;
  informat operations $40. a b c d 8.;&lt;BR /&gt;
  input operations a b c d;&lt;BR /&gt;
  datalines4;&lt;BR /&gt;
a*b;1;2;3;4&lt;BR /&gt;
2*(c*d);1;2;3;4&lt;BR /&gt;
a*b;1;2;3;4&lt;BR /&gt;
a+b+c;1;2;3;4&lt;BR /&gt;
;;;;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=have;&lt;BR /&gt;
  by operations;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
filename gencode temp;&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  set have end=last;&lt;BR /&gt;
  by operations;&lt;BR /&gt;
  &lt;BR /&gt;
  file gencode;&lt;BR /&gt;
/*  file print;*/&lt;BR /&gt;
  if _n_=1 then&lt;BR /&gt;
  do;&lt;BR /&gt;
    put '  select(case);';&lt;BR /&gt;
  end;&lt;BR /&gt;
  if first.operations then&lt;BR /&gt;
  do;&lt;BR /&gt;
    case+1;&lt;BR /&gt;
    put '    when (' case  +(-1) ') do;';&lt;BR /&gt;
    put '      result=' operations ';';&lt;BR /&gt;
    put '    end;';&lt;BR /&gt;
  end;&lt;BR /&gt;
  if last then&lt;BR /&gt;
  do;&lt;BR /&gt;
    put '  end;';&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data want;&lt;BR /&gt;
  set have;&lt;BR /&gt;
  %include gencode;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=want;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Mon, 25 Oct 2010 09:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21616#M4612</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-10-25T09:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Expression in an observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21617#M4613</link>
      <description>Hello Nancy,&lt;BR /&gt;
&lt;BR /&gt;
You can also use the following macro approach:&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
infile datalines dsd dlm=';' truncover;&lt;BR /&gt;
informat op $40. a b c d 8.;&lt;BR /&gt;
input op a b c d;&lt;BR /&gt;
datalines4;&lt;BR /&gt;
a*b;1;2;3;4&lt;BR /&gt;
2*(c*d);1;2;3;4&lt;BR /&gt;
a*b;1;2;3;4&lt;BR /&gt;
a+b+c;1;2;3;4&lt;BR /&gt;
;;;;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* Putting ops into macro variables */;&lt;BR /&gt;
proc SQL;&lt;BR /&gt;
  select count(*) as n into :n       from have;&lt;BR /&gt;
  %let n=%TRIM(&amp;amp;n);&lt;BR /&gt;
  select op       as o into :o1-:o&amp;amp;n from have&lt;BR /&gt;
;quit;&lt;BR /&gt;
&lt;BR /&gt;
/* Using macro variables as operations */; &lt;BR /&gt;
%macro a;&lt;BR /&gt;
%local i;&lt;BR /&gt;
data want;&lt;BR /&gt;
  set have;&lt;BR /&gt;
  %do i=1 %to &amp;amp;n; &lt;BR /&gt;
    if _n_=&amp;amp;i then result=&amp;amp;&amp;amp;o&amp;amp;i; &lt;BR /&gt;
  %end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend a;&lt;BR /&gt;
%a&lt;BR /&gt;
&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Mon, 25 Oct 2010 13:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Expression-in-an-observation/m-p/21617#M4613</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2010-10-25T13:58:49Z</dc:date>
    </item>
  </channel>
</rss>

