BookmarkSubscribeRSS Feed
pavank
Quartz | Level 8
data split_names;
input name $25;
datalines;
JohnDoe
SrinivasPavan
KishoreKumar
ChandrasekaRao
;
run;




fHi Experts,

In the above code i want split names every second capital letter  without regex solution please

 

output
________

John    Doe
Srinivas Pavan
Kishore Kumar
Chandrasekar Rao

 

2 REPLIES 2
Tom
Super User Tom
Super User

Just use some other tool to find the location of the second uppercase letter.

Such as the VERIFY() function.

NOTE: remember to include the period in the format specification, or else your input statement will just read the single byte from column 25. 

 

data split_names;
  input name $25.;
datalines;
JohnDoe
SrinivasPavan
KishoreKumar
ChandrasekaRao
Ghandi
;

data want;
  set split_names;
  loc = verify(substr(name,2),'abcdefghijklmnopqrstuvwzy');
  if loc then last=substr(name,loc+1);
  first=substr(name,1,length(name)-lengthn(last));
run;

proc print;
 var name loc first last;
run;
 Obs    name              loc    first          last

  1     JohnDoe             4    John           Doe
  2     SrinivasPavan       8    Srinivas       Pavan
  3     KishoreKumar        7    Kishore        Kumar
  4     ChandrasekaRao     11    Chandraseka    Rao
  5     Ghandi              6    Ghandi

 

Kathryn_SAS
SAS Employee

Here is an example that adds another case with a middle name.

data split_names;
  input name $25.;
datalines;
JohnDoe
SrinivasPavan
KishoreKumar
ChandrasekaRao
Ghandi
MaryJoeSmith
;
run;

data new;
set split_names;
len=length(name);
ucount = countc(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'U'); /* count the number of uppercase letters */
next=anyupper(substr(name,2));
upper=anyupper(name,-len); /* position of last uppercase letter */
if next=0 then first=name;
else first=substr(name,1,next);
if ucount=2 then last=substr(name,next+1);
else if ucount=3 then do;
 middle=substr(name,next+1,len-upper-1);
 last=substr(name,upper);
end;
run;

proc print data=new;
run;
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1154 views
  • 1 like
  • 3 in conversation