Hello,
Please find below data have and data want.
We have a list of formula using variables such as x, y z.
Note that some rows of the formula file use macros such as movave.
We want all variables in the formula to have brackets, for example [x] instead of x.
There are thousands of variables and thousands of rows with formula.
In addition, we can supply a distinct list of all variables.
In other words, all variables in the formula should be translated to variables with brackets
for example translate x to [x], perhaps like this:-
formula = tranwrd(formula ,'x' ,'[x]' ) ;
data have ;
input formula $200 ;
cards ;
x/y + z
w-x ** z
movave(x)
;
run ;
data want ;
input formula $200 ;
cards ;
[x]/[y] + [z]
[w]-[x] ** [z]
%movave([x])
;
run ;
data variables ;
input without $50 with $50 ;
x [x]
y [y]
z [z]
;
run ;
Thanks,
You could try something like below. It should work as long as you don't have variable names in your formulas with the same name like functions or formats etc.
data have;
infile datalines truncover dsd dlm=',';
input formula_have :$200. formula_want :$200.;
cards;
x/y + z, [x]/[y] + [z]
w-x ** z, [w]-[x] ** [z]
movave(x),%movave([x])
;
run;
data variables;
input without :$50. with :$50.;
datalines;
x [x]
y [y]
z [z]
;
run;
filename codegen temp;
data _null_;
file codegen;
set variables;
cmd= cats('formula_derived=prxchange("s/\b',without,'\b/',with,'/oi",-1,strip(formula_derived));');
put cmd;
run;
data want;
set have;
formula_derived=formula_have;
%include codegen / source2;
run;
proc print data=want;
run;
You could try something like below. It should work as long as you don't have variable names in your formulas with the same name like functions or formats etc.
data have;
infile datalines truncover dsd dlm=',';
input formula_have :$200. formula_want :$200.;
cards;
x/y + z, [x]/[y] + [z]
w-x ** z, [w]-[x] ** [z]
movave(x),%movave([x])
;
run;
data variables;
input without :$50. with :$50.;
datalines;
x [x]
y [y]
z [z]
;
run;
filename codegen temp;
data _null_;
file codegen;
set variables;
cmd= cats('formula_derived=prxchange("s/\b',without,'\b/',with,'/oi",-1,strip(formula_derived));');
put cmd;
run;
data want;
set have;
formula_derived=formula_have;
%include codegen / source2;
run;
proc print data=want;
run;
Thanks a lot it works !
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.