Hi Everyone,
I've a dataset "have " and want to get ouptut given below. Any suggestions regarding this please?
Regards,
Naeem
data have;
infile datalines;
input str $;
datalines;
abcdefgh
bcdeghfa
fghedcbb
ehgbcdae
;
output.
str col1 col2 col3 col4 col5 col6 col7 col8
abcdefgh a b c d e f g h
bcdeghfa b c d e g h f a
fghedcbb f g h e d c b b
ehgbcdae e h g b c d a e
Here is one way. An arbitrary array dimension has been set to 20, but you can also choose the length of the targeting variable as the dimension, that information can be obtained using dictionary tables.
data have;
infile datalines;
input str $;
datalines;
abcdefgh
bcdeghfa
fghedcbb
ehgbcdae
;
data want;
set have;
array col(20) $ 1;
do _n_=1 to lengthn(str);
col(_n_)=char(str,_n_);
end;
run;
Haikuo
Will the string always be the same length?
Congratulations, Reeza! for entering the 10K club!
Haikuo
Hi Reeza,
We may have a variable length for few observarions.
Regards
You can do mass initialization using direct memory access.
Sweet, peek and poke good ol' ZX81 days all over again. Will have to investigate the pokelong function.
Thanks experts - I really appreciate your contribution. This is more than enough for my learning.
Regards,
One more technique sometimes called INFILE magic.
Here is one way. An arbitrary array dimension has been set to 20, but you can also choose the length of the targeting variable as the dimension, that information can be obtained using dictionary tables.
data have;
infile datalines;
input str $;
datalines;
abcdefgh
bcdeghfa
fghedcbb
ehgbcdae
;
data want;
set have;
array col(20) $ 1;
do _n_=1 to lengthn(str);
col(_n_)=char(str,_n_);
end;
run;
Haikuo
Well, quick and dirty method using macro:
data have;
infile datalines;
input str $;
datalines;
abcdefgh
bcdeghfa
fghedcbb
ehgbcdae
;
run;
proc sql;
select LENGTH
into :MAX_LEN
from SASHELP.VCOLUMN
where LIBNAME="WORK"
and MEMNAME="HAVE";
quit;
%macro Parse ();
data want;
set have;
array col{&MAX_LEN.} $1.;
%do i=1 %to &MAX_LEN.;
col&i.=substr(str,&i.,1);
%end;
run;
%mend Parse;
%Parse;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.