BookmarkSubscribeRSS Feed
Discaboota
Obsidian | Level 7

Hello everyone, I am trying to select alternate columns from my dataset, starting from column number 1 and including every nth alternate column after that.
For example, I want to select columns 1, 3, 5, 7, and so on up to column n.
Is there a way to make this selection without having to manually specify the column names?
Here are my column headers:
FINAL_STATUS_01MAY24, FINAL_STATUS_02MAY24, and so on...

I would appreciate any help or advice. Thank you!

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @Discaboota,


@Discaboota wrote:

I am trying to select alternate columns from my dataset, starting from column number 1 and including every nth alternate column after that.
For example, I want to select columns 1, 3, 5, 7, and so on up to column n.


To distinguish between the first and the second "n", let's say we're going to select every kth variable (starting from the first) up to the nth.

Examples of variable numbers to be selected:

1, 3, 5, 7, 9, 11 (k=2, n=11 or 12)
1, 4, 7, 10, 13 (k=3, n=13, 14 or 15)

 

Code:

%let k=2;
%let n=13;

/* Create list of first, (k+1)-th, (2k+1)-th, ... variable name up to the nth */

proc sql noprint;
select nliteral(name) into :varlist separated by ' '
from dictionary.columns
where libname='YOURLIB' & memname='HAVE' & mod(varnum,&k)=1 & varnum<=&n;
quit;

/* Select variables from the prepared list */

data want;
set yourlib.have(keep=&varlist);
run;

Replace "YOURLIB" and "HAVE" with your libref and dataset name (using upper case in the WHERE clause). Make sure that the length of the list of variable names doesn't exceed the maximum length specified in system option MVARSIZE (typically 65534 bytes).

 

PaigeMiller
Diamond | Level 26

No need to ask the same question twice. @ballardw has already given you a very good answer in your other thread. Also, no need to type your subject in ALL CAPITAL LETTERS.

--
Paige Miller
ballardw
Super User

Since your other post indicating this process has to deal with additional columns, which is a stupid way to deal with data as code needs to be modified one way or another for almost any step where the variable names keep changing, perhaps  you should READ YOUR DATA into a proper format to begin with.

 

Preventing problems is usually way easier than fixing them with cumbersome code.

 

This for example reads 5 pairs of variables into a "long" format with on observation per "date". Your implication from your previous and this post is that the poorly designed data contains sequential date values. So this read such.

data example;
  input id @@;
  do date= '01May2024'd to '05May2024'd;
    input final due @@;
    output;
   end;
  format date date9.;
datalines;
1  2 3 4 5 6 7 8 9 10 11
;

I strongly suspect you are starting with some spreadsheet file and don't realize the problems that many people create by the structures such as yours. SAVE the file to CSV, and an infile statement pointing that file and update the values of "date" in the do loop and you may be able to read this into a much more flexible data set:

data example;
  infile "C:\somepath\file.csv"  dlm=',' dsd ;
  input id @@;  /* other variables before the date named columns go here*/
  do date= '01May2024'd to '05May2024'd;
     input final due @@;
     output;
  end;
  format date date9.;
run;

 

 

Ksharp
Super User

Then it is SAS/IML thing.

If these variables are  all numeric or character  type ,you could try this one :

 

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


proc iml;
use have;
read all var _all_ into x[c=vname];
close;
idx=do(1,ncol(x),2); want=x[,idx]; /*Save these desired columns into a dataset named 'WANT'*/ create want from want[c=(vname[idx])]; append from want; close; quit;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 496 views
  • 4 likes
  • 5 in conversation