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!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 950 views
  • 3 likes
  • 3 in conversation