BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Obsidian | Level 7

 

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
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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;

View solution in original post

13 REPLIES 13
Patrick
Opal | Level 21

@pavank The required output you show is what your code creates so what's the question?

pavank
Obsidian | Level 7

Hi Patrick

i give required output below of my code

Patrick
Opal | Level 21

@pavank wrote:

Hi Patrick

i give required output below of my code


@pavank The code you've posted already creates the output that you define as desired. Can you please be very specific what else you're asking for.

pavank
Obsidian | Level 7

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
PaigeMiller
Diamond | Level 26

What is wrong with the output from your code? You haven't told us. Please be specific and detailed.

--
Paige Miller
pavank
Obsidian | Level 7

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

 

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Patrick
Opal | Level 21

@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;
ballardw
Super User

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.

 

pavank
Obsidian | Level 7

 

Hi ballardw  ,

 

Thank you veyr much for your solution

ErikLund_Jensen
Rhodochrosite | Level 12

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?

mkeintz
PROC Star

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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
pavank
Obsidian | Level 7

Hi @mkeintz ,

Thank you very much your solution

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 713 views
  • 1 like
  • 6 in conversation