Home Forums Main Forums SAS Forum How to rename numerous variables in a SAS data set?

  • How to rename numerous variables in a SAS data set?

     Justin updated 3 years, 9 months ago 1 Member · 1 Post
  • Justin

    Administrator
    February 6, 2021 at 11:33 pm

    Q: ” I am wondering who knows how to remove all the underscore in the variable name? I have a data set with more than 200 variables and most of them have more than one underscores. Is there any way to automatically rename all the variables (removing the underscores in all of the variables in the data set) instead of rename them one by one? “

    Under this circumstance, we need to use SAS macro for fulfill it. Below is my code.

     options  mprint  source2  symbolgen;
    data C;
    input A_name $ B_sex $ C_age D_school $;
    cards;
    Jack M 19 Philip
    Sara F 27 Thomas
    ;
    run;

    proc sql;
    create table Var as
    select name
    from sashelp.vcolumn
    where upcase(libname)='WORK' and upcase(memname)='C';
    quit;

    proc contents data=C out=Var_2(keep=name);
    run;

    data combine;
    length combine $1000 combine_2 $1000;
    set Var;
    combine=strip(name)||'='||compress(name,'_') ;
    combine_2=strip(name)||' as '||compress(name,'_');
    run;

    proc sql;
    select combine into: ALL separated by ' '
    from combine;
    quit;
    %put all=*&all*;

    proc sql;
    select combine_2 into: ALL_2 separated by ', '
    from combine;
    quit;
    %put all=*&all_2*;

    data Z;
    set C( rename=(&ALL));
    run;

    proc sql;
    create table Z_2 as
    select &ALL_2
    from C;
    quit;

    Actually, to improve the processing efficiency, it is a much better way to use Proc Dataset than SQL or DATA step to do it. Do you know why?

Log in to reply.

Original Post
0 of 0 posts June 2018
Now