In the below code i want to replace only the last character along with space and double quotes to some other character as below only when there is a space between the last character and double quotes .If there is no space between the last character and double quotes then it should be unchanged.If we perform this with tranwrd then it will change all the spaces and it cannot identify the last character , but PRX functions has that feasibility , but could not able to get the exact Metacharacter to perform this . Tried with Metacharacter [ ] but how to enclose the characters , space and double quotes not sure.
data have ;
length have $100. ;
have='Annual day " ';output;
have='Funny world " ';output;
have='reverse gear "';output;
have =' "Google aps"';output; No space between last character and double quotes
have= '" Android application"';output;
run ;
Required output:
want='Annual dayZZZ"';
want='Funny worldZZZ"';
want='reverse gearZZZ"';
want =' "Google aps"';
want= '" Android application"';
Any help on this.
Hi @keen_sas
You can try this:
data have ;
length have $100. ;
have='Annual day " ';output;
have='Funny world " ';output;
have='reverse gear "';output;
have =' "Google aps"';output;
have= '" Android application"';output;
run ;
data want;
set have;
want = prxchange('s/\s"\s*$/ZZZ"/',1,have);
run;
A systematic, approach using substr:
data have ;
length var $100. ;
var='Annual day " ';output;
var='Funny world " ';output;
var='reverse gear "';output;
var =' "Google aps"';output;
var= '" Android application"';output;
run ;
data want;
set have;
if substr(var,length(var),1)=' ' then substr(var,length(var),1)='';
if substr(var,length(var)-1,1)=' ' then substr(var,length(var)-1,4)='ZZZ"';
run;
data required_output;
length var $100. ;
var='Annual dayZZZ"';output;
var='Funny worldZZZ"';output;
var='reverse gearZZZ"';output;
var =' "Google aps"';output;
var= '" Android application"';output;
run;
proc compare base=want compare=required_output;
run;
-unison
Are those unbalanced quotes a copy/paste error or do you have a single quote in the data?
Hi @keen_sas
You can try this:
data have ;
length have $100. ;
have='Annual day " ';output;
have='Funny world " ';output;
have='reverse gear "';output;
have =' "Google aps"';output;
have= '" Android application"';output;
run ;
data want;
set have;
want = prxchange('s/\s"\s*$/ZZZ"/',1,have);
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.