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

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!

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

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;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

error_prone
Barite | Level 11

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

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

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
  • 557 views
  • 3 likes
  • 5 in conversation