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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.