SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
J111
Quartz | Level 8

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

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;
J111
Quartz | Level 8

Thanks a lot it works !

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 658 views
  • 0 likes
  • 2 in conversation