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?

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: 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?

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: 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
;

 

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: 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


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!

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
  • 896 views
  • 5 likes
  • 3 in conversation