Help using Base SAS procedures

want to take every third variable from the array of variables

Accepted Solution Solved
Reply
New Contributor gim
New Contributor
Posts: 2
Accepted Solution

want to take every third variable from the array of variables

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

;


Accepted Solutions
Solution
‎11-21-2011 05:41 PM
PROC Star
Posts: 7,467

want to take every third variable from the array of variables

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;

View solution in original post


All Replies
Solution
‎11-21-2011 05:41 PM
PROC Star
Posts: 7,467

want to take every third variable from the array of variables

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;

Super Contributor
Posts: 1,636

Re: want to take every third variable from the array of variables

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;

New Contributor gim
New Contributor
Posts: 2

Re: want to take every third variable from the array of variables

Thanks Art. You are great.

Super User
Super User
Posts: 7,035

Re: want to take every third variable from the array of variables

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;

Super User
Posts: 10,018

Re: want to take every third variable from the array of variables

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

Valued Guide
Posts: 765

Re: want to take every third variable from the array of variables

hi ... you could always have SAS write a KEEP statement ...

filename x temp;


data _null_;

file x;

put 'keep';

do j=1 to 13 by 3;

  put 'dfm' j  ' bal' j;

end;

put ';';

run;

data stuff;

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;

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 180 views
  • 8 likes
  • 6 in conversation