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;
data dna2 (drop=i);
set dna;
array d(*) $ d1-d60;
do i=1 to dim(d);
d(i)=substr(dna, i,1);
end;
run;
An easy variation to define each of the new variables as being 1 character long:
array d (*) $ 1 d1-d60;
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
;;;;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.