BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MikeTurner
Calcite | Level 5

I have one dataset including two(x y) or more variables. I want to do some transformation (for examle, power transformation) on my data and get a new dataset including x, y, trans_x and trans_y. Because I probably have more than two variables in my dataset, I wrote one macro.

%macro data_trans(inputdata, outputdata, varlist, trans_factor);

               data &outputdata;

               set &inputdata;

               num=countw("&varlist");

               do i=1 to num;

               'tran_'||left(scan(&varlist,i))= (scan(&varlist,i))**&trans_factor;  /* create one new variable name and do data transformation*/

               end;

               drop num i;

               run;

%mend;

or

%macro data_trans(inputdata, outputdata, varlist, trans_factor);

                  data &outputdata;

                   set &inputdata;

                  array arr &varlist;

                  do over arr;

                   'trans_'||arr=arr**&trans_factor; /* create one new variable name and do data transformation*/

                  end;

                   run;

%mend;

Either of them didn't work. Something wrong was with "||".

Anyone could help me out with it? Greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You need to use the macro statements to generate the variable names.  Your code is trying to use DATA statements.

For example you want the macro to generate the text TRAN_VAR1 so that it can be used on the left side of an assignment statement.

%macro data_trans(inputdata, outputdata, varlist, trans_factor);

data &outputdata;

  set &inputdata;

%do i=1 %to %sysfunc(countw(&varlist));

  tran_%scan(&varlist,&i) = %scan(&varlist,&i)**&trans_factor; 

%end;

run;

%mend;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

You need to use the macro statements to generate the variable names.  Your code is trying to use DATA statements.

For example you want the macro to generate the text TRAN_VAR1 so that it can be used on the left side of an assignment statement.

%macro data_trans(inputdata, outputdata, varlist, trans_factor);

data &outputdata;

  set &inputdata;

%do i=1 %to %sysfunc(countw(&varlist));

  tran_%scan(&varlist,&i) = %scan(&varlist,&i)**&trans_factor; 

%end;

run;

%mend;

Linlin
Lapis Lazuli | Level 10

assuming you want to transform all variables except  variable 'aa':

data have;

input x y z aa;

cards;

1 2 2 20

3 4 3 30

;

%let trans_factor=2;

proc sql noprint;

  select catt(name,',',name,'**',&trans_factor, ' as',catt(' trans_',name)) into: names separated by ','

  from dictionary.columns

    where libname='WORK' and memname='HAVE'  and upcase(name) ne 'AA';

quit;

%put &names;

proc sql;

  create table want as select &names

    from have;

quit;

proc print;run;

             Obs    x    trans_x    y    trans_y    z    trans_z

                 1     1       1       2        4      2       4

                 2     3       9       4       16      3       9

Linlin

MikeTurner
Calcite | Level 5

Thanks, all guys!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1297 views
  • 3 likes
  • 3 in conversation