Desktop productivity for business analysts and programmers

Array help needed: sequence assigned to variables

Reply
Occasional Contributor
Posts: 14

Array help needed: sequence assigned to variables

y sub
Hello,

My task is taking DNA sequences that are 60 characters long and creating 60 different variables (e.g. d1 - d60).

Variable D1 holds the DNA at the first position, D2 at the 2nd position...all the way to D60 at the 60th position.

I'm attaching the code below I have so far with some a data sample - I'm able to create the 60 variables but cannot get them populated correctly.

Any advice is much appreciated.

Regards,

Karen

data dna;

length dna $ 60;

input dna $;

datalines;

TGGAAGGGCTAATTTGGTCCCAAAAAAGACAAGAGATCCTTGATCTGTGGATCTACCACA


TGATTGGCAGAACTACACACCAGGGCCAGGGATCAGATATCCACTGACCTTTGGATGGTG

;

data dna2 (drop=i);

set dna;

counter=0;

array d(*) d1-d60;

do i=1 to dim(d);

if d(i) in ('C','A','T','G') then counter + 1;

substr(dna, I, 1)=d(i);

end;

run;

















Super User
Posts: 19,170

Re: Array help needed: sequence assigned to variables

data dna2 (drop=i);

set dna;


array d(*) $ d1-d60;

do i=1 to dim(d);


d(i)=substr(dna, i,1);

end;

run;

Super User
Posts: 5,369

Re: Array help needed: sequence assigned to variables

An easy variation to define each of the new variables as being 1 character long:

array d (*) $ 1 d1-d60;

Super User
Super User
Posts: 6,848

Re: Array help needed: sequence assigned to variables

If you are reading from a raw file then read it directly into the array.

data want ;

   input (d1-d60) ($1.) ;

   count=lengthn(_infile_);

cards;

TGGAAGGGCTAATTTGGTCCCAAAAAAGACAAGAGATCCTTGATCTGTGGATCTACCACA

TGATTGGCAGAACTACACACCAGGGCCAGGGATCAGATATCCACTGACCTTTGGATGGTG

;;;;

If you already have a variable you can use a dummy input record to trick SAS into letting you use an INPUT statement.

data want ;

   set have ;

   input @@ ;

   _infile_ = dna ;

   input @1 (d1-d60) ($1.) @@ ;

   count=lengthn(dna);

cards;

DUMMY RECORD

;;;;

Occasional Contributor
Posts: 14

Re: Array help needed: sequence assigned to variables

Thanks everyone for your help!

I finally tweaked it:

data dna2;

set dna;

array d{60} $1 D1-D60;

do i= 1 to 60;

d{i}=substr(dna,i,1);

end;

drop dna i;

run;

Ask a Question
Discussion stats
  • 4 replies
  • 413 views
  • 7 likes
  • 4 in conversation