BookmarkSubscribeRSS Feed
Nancy
Calcite | Level 5
Hi,

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?

Thanks for your help,

Nancy
2 REPLIES 2
Patrick
Opal | Level 21
Hi Nancy

An approach like in the code below could work for you.

data have;
infile datalines dsd dlm=';' truncover;
informat operations $40. a b c d 8.;
input operations a b c d;
datalines4;
a*b;1;2;3;4
2*(c*d);1;2;3;4
a*b;1;2;3;4
a+b+c;1;2;3;4
;;;;
run;

proc sort data=have;
by operations;
run;

filename gencode temp;

data have;
set have end=last;
by operations;

file gencode;
/* file print;*/
if _n_=1 then
do;
put ' select(case);';
end;
if first.operations then
do;
case+1;
put ' when (' case +(-1) ') do;';
put ' result=' operations ';';
put ' end;';
end;
if last then
do;
put ' end;';
end;
run;

data want;
set have;
%include gencode;
run;

proc print data=want;
run;


HTH
Patrick
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Nancy,

You can also use the following macro approach:

data have;
infile datalines dsd dlm=';' truncover;
informat op $40. a b c d 8.;
input op a b c d;
datalines4;
a*b;1;2;3;4
2*(c*d);1;2;3;4
a*b;1;2;3;4
a+b+c;1;2;3;4
;;;;
run;

/* Putting ops into macro variables */;
proc SQL;
select count(*) as n into :n from have;
%let n=%TRIM(&n);
select op as o into :o1-:o&n from have
;quit;

/* Using macro variables as operations */;
%macro a;
%local i;
data want;
set have;
%do i=1 %to &n;
if _n_=&i then result=&&o&i;
%end;
run;
%mend a;
%a

Sincerely,
SPR

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 728 views
  • 0 likes
  • 3 in conversation