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

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

;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

6 REPLIES 6
art297
Opal | Level 21

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;

Linlin
Lapis Lazuli | Level 10

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;

gim
Calcite | Level 5 gim
Calcite | Level 5

Thanks Art. You are great.

Tom
Super User Tom
Super User

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;

Ksharp
Super User
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

MikeZdeb
Rhodochrosite | Level 12

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 6 replies
  • 767 views
  • 8 likes
  • 6 in conversation