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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1038 views
  • 0 likes
  • 3 in conversation