BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
flh3
Fluorite | Level 6

Is it possible to select/keep columns in SAS by using an index like in R? (e.g., I want to keep columns 2 through 5 in a dataset with 10 variables). I want to do this without specifying the names (e.g., keep x1 x2 x3).

In R, to see only those columns (without knowing the column names), I would type x[,2:5]

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

You could use proc sql to create a macro variable.  e.g.:

proc sql;

  select name

    into :vars separated by ' '

      from dictionary.columns

        where libname="SASHELP" and /*must be upper case*/

              memname="CLASS" and /*must be upper case*/

              varnum between 2 and 5

  ;

quit;

data want;

  set sashelp.class (keep=&vars.);

run;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

You could use proc sql to create a macro variable.  e.g.:

proc sql;

  select name

    into :vars separated by ' '

      from dictionary.columns

        where libname="SASHELP" and /*must be upper case*/

              memname="CLASS" and /*must be upper case*/

              varnum between 2 and 5

  ;

quit;

data want;

  set sashelp.class (keep=&vars.);

run;

Rick_SAS
SAS Super FREQ

An interesting question.  If you know the 2nd and 5th names, you can use the -- operator to get the variable names between them.  I didn't know how to do the general case, but I found a useful paper by Stephanie Thompson that describes how to use PROC SQL to do this operation: http://analytics.ncsu.edu/sesug/2006/CC01_06.PDF

Here is one solution:

proc contents data = Sashelp.Cars noprint out=vars(keep = varnum name);
run;

proc sql noprint;
   select name
   into :keepvars separated by ' '
   from vars
   where 2 <= varnum <= 5
   order by varnum;
quit;

%put &keepvars;

data subset;
   set Sashelp.Cars;
   keep &keepvars;
run;

flh3
Fluorite | Level 6

Thanks Art and Rick for the answers! Both solutions work (I was hoping for a solution that didn't use SQL but that's fine).

Jagadishkatam
Amethyst | Level 16

Hi ,

Here is an approach of getting the desired output but from datastep without the use of proc sql.

data class;

set sashelp.class;

run;

data columns;

set sashelp.vcolumn;

where libname='WORK' and memname='CLASS' and 2<=varnum<=5;

call symputx("keep"||trim(put(varnum,1.)),name);

run;

%put &keep2 &keep3;

%macro vars;

data class1;

set class;

keep %do j=2 %to 5; &&keep&j %end;;

run;

%mend;

%vars

hope it helps.

Thanks,

Jag

Thanks,
Jag
Tom
Super User Tom
Super User

If you want to treat a series of variables as if they form a matrix then why not just name them using numeric suffixes?  Such X1-X10.  Then it is as easy to select as typing in the index numbers. You can use the LABELS to store more meaning information about the variables.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 9439 views
  • 2 likes
  • 5 in conversation