BookmarkSubscribeRSS Feed
Steelers_In_DC
Barite | Level 11

It doesn't matter if using infile/input, proc sql or a data step.  I am interested in selecting a column by it's position and renaming it.  I will have a SAS dataset where the column names might change, one is only 2*3, very small but I have to reference it and cannot bet on the variable names being constant going forward.   Any way to select the variable names by ordinal position and rename them?

Thanks,

Mark

2 REPLIES 2
data_null__
Jade | Level 19

In a data step you can define an ARRAY and reference the columns by their index.  Say you want to work with numeric column number 3.

data reference;
   set sashelp.heart;
   array _n
  • _numeric_;
      
    * Reference to numeric column 3;
      
    if _n[3] lt 50;
      
    run;
    data_null__
    Jade | Level 19

    Or you can rename all the variable using their column index.

    data heart;
       set sashelp.heart(obs=10);
       run;

    proc transpose data=heart(obs=0 /*keep=_numeric_*/) out=vars;
       var _all_;
       run;
    filename FT76F001 temp;
    data _null_;
      
    file FT76F001;
       set vars;
       put 'Rename ' _name_ '=Col' _n_ ';';
      
    run;

    proc datasets;
      
    modify heart;
       %inc FT76F001;
       run;
      
    quit;
    proc print data=heart;
       run;

    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!

    How to Concatenate Values

    Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

    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
    • 2 replies
    • 18434 views
    • 0 likes
    • 2 in conversation