How to split a sas dataset columnwise

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

How to split a sas dataset columnwise

I have following code:

 

data tmp;
pk_consumer_id = 10;
bk_consumer_id = '10';
pk_country_id = 'uk';
fk_consumer_id = 10;
var_1 = 'a';
var_2 = 'b';
var_3 = 'c';
var_4 = 'd';
var_5 = 'e';
run;

data _null_;
set tmp;
array nums(*) _numeric_;
array chars(*) _character_;
n_col = dim(nums)+dim(chars);
call symput('n_col',n_col);
run;

My aim now is to split original file (tmp) into subsets with 2 columns with out knowing the names of variables within tmp (i.e. would like to reference the cols numerically). Is there any easy way how to proceed? Something like:

 

data want;

set tmp (keep=1:2);

run;

 

Thanks for your suggestions!

 

 

 

 


Accepted Solutions
Solution
‎11-02-2017 04:49 AM
Trusted Advisor
Posts: 1,145

Re: How to split a sas dataset columnwise

Posted in reply to Uknown_user

This might be a good application for CALL VNEXT, which allows you to retrieve the name, type, and length progressing from the first to last variable in the PDV.

 

filename tmp1 temp;

data _null_;
  if 0 then set sashelp.class;
  length varname $32;
  file tmp;
  do I=1 to 2;
    call vnext(varname);
    put varname @;
  end;
run;

data want;
  set sashelp.class;
  keep %include tmp /source2;  ;
run;

View solution in original post


All Replies
PROC Star
Posts: 432

Re: How to split a sas dataset columnwise

[ Edited ]
Posted in reply to Uknown_user

use varnum in dictionary columns something like shown below. this is untested code

proc sql;
select name :col separated by ' '
 from dictionary.columns
 where memname = upcase("yourdatset")
and libname =upcase("yourlibname")
and varnum between 1 and 2;/*variables you want want 
 quit;


data want;
set have( drop = &col);
run;

 

Super User
Super User
Posts: 8,634

Re: How to split a sas dataset columnwise

[ Edited ]
Posted in reply to Uknown_user

Not really, it rarely makes sense to split data out. To do something like this you would need to generate either the code as a whole or the keep lists, e.g.

data _null_; 
  length klist $2000; 
  set sashelp.vcolumn (where=(libname="WORK" and memname="HAVE")); 
  retain klist; 
  retain i 1; 
  if mod(varnum,2)=0 then do; 
    klist=catx(" ",klist,name); 
    call execute('data want'||strip(put(i,best.))||"; set have (keep="||strip(klist)||"); run;"); 
    i=i+1; 
  end; 
  else do; 
    klist=catx(" ","pk_consumer_id bk_consumer_id pk_country_id fk_consumer_id",name);
  end; 
run;

Change work to your library and memname to your dataset.

Regular Contributor
Posts: 185

Re: How to split a sas dataset columnwise

Posted in reply to Uknown_user

You could rename all columns to var1 - varX, but this will lead to hardly readable code. Why do you want to split, at all?

Solution
‎11-02-2017 04:49 AM
Trusted Advisor
Posts: 1,145

Re: How to split a sas dataset columnwise

Posted in reply to Uknown_user

This might be a good application for CALL VNEXT, which allows you to retrieve the name, type, and length progressing from the first to last variable in the PDV.

 

filename tmp1 temp;

data _null_;
  if 0 then set sashelp.class;
  length varname $32;
  file tmp;
  do I=1 to 2;
    call vnext(varname);
    put varname @;
  end;
run;

data want;
  set sashelp.class;
  keep %include tmp /source2;  ;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 173 views
  • 3 likes
  • 5 in conversation