BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
keen_sas
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

 Capture d’écran 2019-12-12 à 17.56.56.png

 

View solution in original post

3 REPLIES 3
unison
Lapis Lazuli | Level 10

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

-unison
andreas_lds
Jade | Level 19

Are those unbalanced quotes a copy/paste error or do you have a single quote in the data?

ed_sas_member
Meteorite | Level 14

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;

 Capture d’écran 2019-12-12 à 17.56.56.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 476 views
  • 1 like
  • 4 in conversation