sas

Remove a specific suffix in variable names


Suppose I have the following dataset:

ID       GEO    Variable_1    Variable_name_1     Variable_code_as_1
1         A       0.3             0.0                 1.3             
1         B       0.1             0.2                 1.6
1         D       0.22            0.5                 1.2
1         B       0.5             0.8                 1.4
1         C       0.6             0.1                 1.7
1         F       0.9             0.2                 1.8

Is there a way to remove "underscore 1" from variable names? I tried different codes found all around on the web but without success maybe because there are different "_" and I need to remove only the last one.

Edit: need to remove _1


Solution

  • Try this

    data have;
    input ID GEO $ Variable_1 Variable_name_1 Variable_code_as_1;
    datalines;
    1 A 0.3  0.0 1.3 
    1 B 0.1  0.2 1.6
    1 D 0.22 0.5 1.2
    1 B 0.5  0.8 1.4
    1 C 0.6  0.1 1.7
    1 F 0.9  0.2 1.8
    ;
    
    proc contents data=have;run;
    
    data _null_;
       set sashelp.vcolumn end=lr;
       where libname='WORK' and memname='HAVE';
    
       if _n_ = 1 then 
          call execute('proc datasets lib=work nolist; modify have;');
          
       if substr(name, length(name) -1, 2) = '_1' then 
          call execute('rename ' || name || '=' || tranwrd(name, '_1', '') ||';');
    
       if lr then 
          call execute('quit;');
    run;
    
    proc contents data=have;run;