data vertical_split;
name = 'William Shakespeare';
length character $30;
do i = 1 to length(name);
character = substr(name, i, 1);
output;
end;
drop i;
run;
proc print data=vertical_split noobs;
run;
required output
ame | character |
---|---|
William | W |
William | i |
William | l |
William | l |
William | i |
William | a |
William | m |
Shakespeare | S |
Shakespeare | h |
Shakespeare | a |
Shakespeare | k |
Shakespeare | e |
Shakespeare | s |
Shakespeare | p |
Shakespeare | e |
Shakespeare | a |
Shakespeare | r |
Shakespeare | e |
@pavank I missed to see that you only want part of the name. As a tip: If someone doesn't understand what you are saying/asking don't just repeat the same again but express it in different words.
data vertical_split;
length name part_name $40;
name = 'William Shakespeare';
length character $1;
name=compbl(name);
do i = 1 to length(name);
character = substr(name, i, 1);
if not missing(character) then part_name=scan(name,part_no+1,' ');
else do; part_no+1; call missing(part_name); end;
output;
end;
drop i part_no;
run;
proc print data=vertical_split noobs;
run;
@pavank The required output you show is what your code creates so what's the question?
Hi Patrick
i give required output below of my code
my code give below output but it's not required output
name | character |
---|---|
William Shakespeare | W |
William Shakespeare | i |
William Shakespeare | l |
William Shakespeare | l |
William Shakespeare | i |
William Shakespeare | a |
William Shakespeare | m |
William Shakespeare | |
William Shakespeare | S |
William Shakespeare | h |
William Shakespeare | a |
William Shakespeare | k |
William Shakespeare | e |
William Shakespeare | s |
William Shakespeare | p |
William Shakespeare | e |
William Shakespeare | a |
William Shakespeare | r |
William Shakespeare | e |
What is wrong with the output from your code? You haven't told us. Please be specific and detailed.
Hi Patrick
i want below output
name | character |
William | W |
William | i |
William | l |
William | l |
William | i |
William | a |
William | m |
Shakespeare | S |
Shakespeare | h |
Shakespeare | a |
Shakespeare | k |
Shakespeare | e |
Shakespeare | s |
Shakespeare | p |
Shakespeare | e |
Shakespeare | a |
Shakespeare | r |
Shakespeare | e |
It would help if you explain in words, instead of pasting in the same output as before which we didn't understand. But I think I see why you said the output is wrong.
So let me ask you something. At no point in your code have you even tried to write code that splits William and Shakespeare. Could you take a crack at that?
@pavank I missed to see that you only want part of the name. As a tip: If someone doesn't understand what you are saying/asking don't just repeat the same again but express it in different words.
data vertical_split;
length name part_name $40;
name = 'William Shakespeare';
length character $1;
name=compbl(name);
do i = 1 to length(name);
character = substr(name, i, 1);
if not missing(character) then part_name=scan(name,part_no+1,' ');
else do; part_no+1; call missing(part_name); end;
output;
end;
drop i part_no;
run;
proc print data=vertical_split noobs;
run;
A slightly different approach:
data vertical_split; name = 'William Shakespeare'; length nameword $ 30 character $ 1; do i = 1 to countw(name,' '); nameword=scan(name,i,' '); do j=1 to length(nameword); character = substr(nameword, j, 1); output; end; /* to put the blank row between "words" of the name*/ if i < countw(name,' ') then do; call missing(nameword,character); output; end; end; drop i j name; run;
Using SCAN to get space-delimited "words" from the name to get the partial name and then write out the characters of that partial name.
Insert a space line between the words.
Hi ballardw ,
Thank you veyr much for your solution
Hi @pavank
Just out out of curiosity, why do dou want to split the name in single letters while keeping embedded spaces without a name reference? - It beats me what that would lead to, so would you care to give some context information and tell us about your final goal?
Here's an alternative whose primary (only?) advantage is that the code has fewer semicolons:
data vertical_split (drop=L name);
name = 'William Shakespeare';
do L=1 to length(name);
LTR=char(name,L);
length subname $40;
if subname=' ' and LTR^=' ' then subname=scan(substr(name,L),1);
else if LTR=' ' then subname=' ';
output;
end;
run;
Code above is modified slightly to accommodate subnames separated by more than one blank.
Hi @mkeintz ,
Thank you very much your solution
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.