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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 5 replies
  • 8967 views
  • 2 likes
  • 5 in conversation