Help using Base SAS procedures

How to use "||" sign to get one new variable name?

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

How to use "||" sign to get one new variable name?

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.


Accepted Solutions
Solution
‎02-05-2012 09:14 AM
Super User
Super User
Posts: 6,502

Re: How to use "||" sign to get one new variable name?

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


All Replies
Solution
‎02-05-2012 09:14 AM
Super User
Super User
Posts: 6,502

Re: How to use "||" sign to get one new variable name?

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;

Super Contributor
Posts: 1,636

Re: How to use "||" sign to get one new variable name?

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

Contributor
Posts: 49

Re: How to use "||" sign to get one new variable name?

Thanks, all guys!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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