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
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?
If you don't want to split based on upper case letter, what logic can you use to split in the appropriate location?
@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.
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
;
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 thisfirst last
Allu Arjun
Pavan Kalyan
Rangaiah Naidu
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Ready to level-up your skills? Choose your own adventure.