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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.