Select some columns

Reply
Frequent Contributor
Posts: 81

Select some columns

Hello everyone!  Is there any way to extract data files by select some of columns? Here it is a sample data:


Data have;

Col1  Col2  Col3   Col4   Col5   Col6   Col7   Col8   Col9     Coln

1.2    -0.2    0.003  1.5       2.3      3.6      -2.8     1.7      5.8      .......

2.6    3.1     0.15    0.56     0.15     1.1      1.9     -2.1     1.4      .......

And here it is what I want to do:


Data have1:                            Data have2:                                           Data have3;

Col1  Col4  Col7                     Col2   Col5     Col8                                Col3     Col6       Col9

1.2     1.5    -2.8                      -0.2      2.3        1.7                                    0.003    3.6        5.8

2.6     0.56   1.9                       3.1     0.15       -2.1                                   0.15      1.1        1.4

I will appreciate any kind of help!

Zana


Super User
Posts: 17,868

Re: Select some columns

You can do it manually using a keep statement.

If you need to do it automatically, its a bit harder but this seems like a weird way to split data. Can you explain further what you're trying to do?

data have1;

set have;

keep col1 col4 col7;

run;

data have2;

set have;

keep col2 col5 col7;

run;

Frequent Contributor
Posts: 81

Re: Select some columns

Dear Reeza i have more columns (730 columns), so keep or drop statement will not be suitable.

Super User
Posts: 17,868

Re: Select some columns

What's the pattern in the variable names? Look at sashelp.vcolumns and you can create macro variables that list your column names.

Super User
Super User
Posts: 6,502

Re: Select some columns

You can use the variable number from the metadata to generate the list of variable names into macro variables.

data have ;

input col1-col9;

cards;

1.2 -0.2 0.003 1.5 2.3 3.6 -2.8 1.7 5.8

2.6 3.1 0.15 0.56 0.15 1.1 1.9 -2.1 1.4

;;;;


proc contents data=have noprint out=contents(keep=varnum name);

run;


proc sql noprint ;

select case when mod(varnum,3)=1 then name else ' ' end

     , case when mod(varnum,3)=2 then name else ' ' end

     , case when mod(varnum,3)=0 then name else ' ' end

   into :list1 separated by ' '

     , :list2 separated by ' '

     , :list3 separated by ' '

from contents

;

quit;


data want1 (keep=&list1)

     want2 (keep=&list2)

     want3 (keep=&list3)

;

  set have;

run;


Super User
Posts: 17,868

Re: Select some columns

Would the 4th data set start at col4 or col10?

Ask a Question
Discussion stats
  • 5 replies
  • 253 views
  • 0 likes
  • 3 in conversation