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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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