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
Super User

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?

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
112211
Obsidian | Level 7
no
Quentin
Super User

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

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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
Super User

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
;

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 1197 views
  • 5 likes
  • 3 in conversation