- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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,
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot it works !