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

## character functions

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
Obsidian | Level 7

6 REPLIES 6
PROC Star

## Re: character functions

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.
Obsidian | Level 7

no
PROC Star

## Re: character functions

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

## Re: character functions

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

PROC Star

## Re: character functions

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

## Re: character functions

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

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