Pranasas
Pranasas

Reputation: 596

How to insert dash symbols into character variables (SAS)?

So in SAS I have a length 10 character-type variable fooword. What I want to accomplish is that when a custom format is applied

format fooword $fooformat.;

dash symbols are displayed after every two symbols (from right to left). In other words BERRY would look like B-ER-RY and STRAWBERRY would look like ST-RA-WB-ER-RY.

Surely I can create a new longer variable and add dashes by using a bunch of substr() but that looks like a terrible solution in my eyes.

So...

proc format;
    value $fooformat
    ???
    ;
run;

Upvotes: 1

Views: 4052

Answers (1)

Joe
Joe

Reputation: 63424

Not possible with a format, sadly. Possible with a FCMP function or a regex. A regex that might work would be like so:

data test;
length origtext fintext $50;
input origtext $;
fintext=prxchange('s/([a-z]{2})(?=[a-z])/$1-/io',-1,origtext);
put origtext= fintext=;
datalines;
BLACKHOLES
GRAVITYWELL
DOREMIFASOLATI
;;;;
run;

Looks for 2 alpha characters, then a positive lookahead for another alpha character (so it doesn't throw one onto the end), then replaces with same plus a dash. You could make a FCMP function that either did this or simply iterated through the string placing dashes throughout. That would be more similar to the format in usage, if not in actual details.

Upvotes: 1

Related Questions