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!
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;
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;
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.
You could rename all columns to var1 - varX, but this will lead to hardly readable code. Why do you want to split, at all?
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.