HI
I have hundred of variable. I provide a smaller dataset for the illustration purpose.And I want to make it through Arrays.I want to take every third variable from the array of variables Dfm and bal series starting from first varible.
Thanks in advance.
Data
I_have;
input
Dfm1$ Dfm2$ Dfm3$ Dfm4$ Dfm5$ Dfm6$ Dfm7$ Dfm8$ Dfm9$ Dfm10$ Dfm11$ Dfm12$ Dfm13$ Dfm14$ bal1 bal2 bal3 bal4 bal5 bal6 bal7 bal8 bal9 bal10 bal11 ball2 bal13 ball4;
Datalines
;
Y Y Y Y Y N Y N Y N Y Y N y 22 12 14 151 56 58 56 89 87 45 63 26 111 78
;
;
run;
Data
I_wanna;
input
Dfm1$ Dfm4$ Dfm7$ Dfm10$ Dfm13$ bal1 bal4 bal7 bal10 bal13;
Datalines
;
Y Y Y N N 22 151 56 45 111
;
run
;
Sorry, but today appears to be one of my brute force days. However, I think that the following accomplishes what you want:
data I_have;
input (Dfm1-Dfm14 bal1-bal14)($);
Datalines;
Y Y Y Y Y N Y N Y N Y Y N y 22 12 14 151 56 58 56 89 87 45 63 26 111 78
;
data _null_;
length keep $32767;
set I_have;
array d(*) dfm:;
array b(*) bal:;
if _n_ eq 1 then do;
do i=1 to dim(d);
if mod(i,3) eq 1 then do;
keep=catx(' ',keep,vname(d(i)));
end;
end;
do i=1 to dim(b);
if mod(i,3) eq 1 then do;
keep=catx(' ',keep,vname(b(i)));
end;
end;
end;
call symput('keep',put(keep,$32767.));
stop;
run;
data I_wanna;
set I_have (keep=&keep.);
run;
Sorry, but today appears to be one of my brute force days. However, I think that the following accomplishes what you want:
data I_have;
input (Dfm1-Dfm14 bal1-bal14)($);
Datalines;
Y Y Y Y Y N Y N Y N Y Y N y 22 12 14 151 56 58 56 89 87 45 63 26 111 78
;
data _null_;
length keep $32767;
set I_have;
array d(*) dfm:;
array b(*) bal:;
if _n_ eq 1 then do;
do i=1 to dim(d);
if mod(i,3) eq 1 then do;
keep=catx(' ',keep,vname(d(i)));
end;
end;
do i=1 to dim(b);
if mod(i,3) eq 1 then do;
keep=catx(' ',keep,vname(b(i)));
end;
end;
end;
call symput('keep',put(keep,$32767.));
stop;
run;
data I_wanna;
set I_have (keep=&keep.);
run;
Hi Art,
I made some changes to your code. Happy Thanksgiving!!!
/*********************************/
data _null_;
length kp $32767;
set I_have;
array d(*) dfm:;
array b(*) bal:;
do i=1 to dim(d) by 3;
kp=catx(' ',kp,vname(d(i)),vname(b(i)));
end;
call symput('kp',put(kp,$32767.));
stop;
run;
data wanna;
set I_have (keep=&kp);
run;
Thanks Art. You are great.
What do you mean by "take"? What are you going to do with them?
Why not normalize the dataset? Then you could "take" them by using a WHERE clause.
data vertical ;
set have ;
array _1 dfm: ;
array _2 bal: ;
do week = 1 to dim(_1);
dfm = _1(week);
bal = _2(week);
output;
end;
keep week dfm bal;
run;
data want ;
set vertical;
where mod(week,3)=1;
run;
Data have; input Dfm1$ Dfm2$ Dfm3$ Dfm4$ Dfm5$ Dfm6$ Dfm7$ Dfm8$ Dfm9$ Dfm10$ Dfm11$ Dfm12$ Dfm13$ Dfm14$ bal1 bal2 bal3 bal4 bal5 bal6 bal7 bal8 bal9 bal10 bal11 ball2 bal13 bal14; Datalines; Y Y Y Y Y N Y N Y N Y Y N y 22 12 14 151 56 58 56 89 87 45 63 26 111 78 ; run; data _null_; set sashelp.vcolumn(keep=libname memname name where=(libname='WORK' and memname='HAVE')) end=last; if _n_ eq 1 then call execute('data want;set have;keep '); if mod(input(substr(name,anydigit(name)),best8.),3) eq 1 then call execute(name); if last then call execute(';run;'); run;
Ksharp
hi ... you could always have SAS write a KEEP statement ...
data _null_;
file x;
put 'keep';
do j=1 to 13 by 3;
put 'dfm' j ' bal' j;
end;
put ';';
run;
input (dfm1-dfm14) (: $1.) bal1-bal14;
%include x ;
datalines;
Y Y Y Y Y N Y N Y N Y Y N y 22 12 14 151 56 58 56 89 87 45 63 26 111 78
;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.