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

data gfg;
length full_name $20.;
input full_name $;
first_name = substr(full_name, 1, floor(length(full_name)/2));
last_name = substr(full_name, floor(length(full_name)/2)+1);
datalines;
AlluArjun
PavanKalyan
RangaiahNaidu
;
run; I had tried this code but didn't get my desired output .I want output should be like this   

first       last
Allu           Arjun
Pavan       Kalyan
Rangaiah Naidu

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
Quentin
PROC Star

You're splitting the names based on the midpoint of the string.  So it results in:

 

                        first_     last_
Obs      full_name       name      name

 1     AlluArjun        Allu      Arjun
 2     PavanKalyan      Pavan     Kalyan
 3     RangaiahNaidu    Rangai    ahNaidu

You'll need some other logic for splitting the names.  It for the sample you showed, perhaps you could split the string at the location of the second uppercase letter.  Would that work for your data?

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
112211
Obsidian | Level 7
no
Quentin
PROC Star

If you don't want to split based on upper case letter, what logic can you use to split in the appropriate location?

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
ballardw
Super User

@112211 wrote:
no

Really , you marked your own answer of NO to a question as an accepted solution. Please describe how that is an actual solution.

Quentin
PROC Star

If you like the idea of splitting based on the uppercase letter, you could do it like:

 

data gfg;
length full_name $20 ;
input full_name $;

split=findc(full_name,,'U',2) ; *position of first uppercase letter found, starting search at position 2 of the string ;

first_name = substr(full_name, 1, split-1);
last_name = substr(full_name, split);

datalines;
AlluArjun
PavanKalyan
RangaiahNaidu
;

 

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
ballardw
Super User

Specify a RULE for programming. Examples help but do not define a RULE.

Programs use rules. I can make your example work but it would not work for any other values than the ones shown because it would work for example values.

 

If the "rule" is the "the last name is the second occurrence of a capital letter" then the code means "identify the position of the second capital letter". Which leads to possible use of the ANYUPPER function .

data gfg;
length full_name $20.;
input full_name $;
secondcapital = anyupper(substr(full_name,2))+1;
first= substr(full_name, 1,secondcapital-1);
last= substr(full_name, secondcapital);
datalines;
AlluArjun
PavanKalyan
RangaiahNaidu
;

We search the full name starting at position 2 because the function Anyupper returns the first position of an upper case letter. Then add 1 to get the actual position since the first character was ignored.

Then use that separate the words.

Note: if there is no second capital this will throw an error because the position won't be found and you will need to define a different rule for that case.

 


@112211 wrote:

data gfg;
length full_name $20.;
input full_name $;
first_name = substr(full_name, 1, floor(length(full_name)/2));
last_name = substr(full_name, floor(length(full_name)/2)+1);
datalines;
AlluArjun
PavanKalyan
RangaiahNaidu
;
run; I had tried this code but didn't get my desired output .I want output should be like this   

first       last
Allu           Arjun
Pavan       Kalyan
Rangaiah Naidu


 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 527 views
  • 5 likes
  • 3 in conversation