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

Can anyone help me with this little puzzle?  I'm trying to use capture buffers to perform conditional replacement of multiple values in a single pass.  The replaced values are two characters (versus the single character I'm matching) so I can't use translate.  I could use multiple instances of tranwrd (or nest the prxchange), but I'm keen to know - is a single pass possible?  I do not know the length or content of my input values.

 

data _null_;
  have=prxchange('s/a|g|f/$1aa$2gg$3ff/',-1,'sasgf');
  put have=;
  put 'want=saasggff';
run;

result:

 have=saaggffsaaggffaaggff
 want=saasggff

 

Edit - to clarify, the values on the right should be anything, eg: 

 

 73         data _null_;
 74           have=prxchange('s/a|g|f/$1xx$2yy$3zz/',-1,'sasgf');
 75           put have=;
 76           put 'want=sxxsyyzz';
 77         run;
 
 have=sxxyyzzsxxyyzzxxyyzz
 want=sxxsyyzz
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @AllanBowe 

 

Here is an attempt to achieve this:

data ref;
	input match $ replace $;
	datalines;
a xx
g yy
f zz
;	

data _null_;
	set ref end=eof;
	length have $ 30;
	if _n_=1 then have = 'sasgf';
	retain have;
	have=prxchange(cats('s/',match,'/',replace,'/'),-1,have);
	if eof then do;
		put have=;
		put 'want=sxxsyyzz';
	end;
run;

My best,

 

View solution in original post

8 REPLIES 8
ed_sas_member
Meteorite | Level 14

Hi @AllanBowe 

Does this answer your question?

data _null_;
  have=prxchange('s/(a|g|f)/$1$1/',-1,'sasgf');
  put have=;
  put 'want=saasggff';
run;
 71         data _null_;
 72           have=prxchange('s/(a|g|f)/$1$1/',-1,'sasgf');
 73           put have=;
 74           put 'want=saasggff';
 75         run;
 
 have=saasggff
 want=saasggff
AllanBowe
Barite | Level 11

thanks for the quick response! You solved the puzzle (as it was set) but I'm still stuck. The replacement values could be anything (I should have picked better examples)

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
ed_sas_member
Meteorite | Level 14

No problem Smiley Happy

 

Could you please provide some representative sample data?

AllanBowe
Barite | Level 11

done

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
ed_sas_member
Meteorite | Level 14

Hi @AllanBowe 

 

Here is an attempt to achieve this:

data ref;
	input match $ replace $;
	datalines;
a xx
g yy
f zz
;	

data _null_;
	set ref end=eof;
	length have $ 30;
	if _n_=1 then have = 'sasgf';
	retain have;
	have=prxchange(cats('s/',match,'/',replace,'/'),-1,have);
	if eof then do;
		put have=;
		put 'want=sxxsyyzz';
	end;
run;

My best,

 

AllanBowe
Barite | Level 11

ha ha - ok.  From what I can tell, it is not possible to do what I am trying to do.  But I admire the approach you took to avoid nesting the regex calls 🙂

 

accepted.

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
ed_sas_member
Meteorite | Level 14
You’re welcome 😊 !
rafgag
Calcite | Level 5

Hi @AllanBowe,

Is this what you were looking for?

 

28         data _null_;

29         /*  have=prxchange('s/a|g|f/$1aa$2gg$3ff/',-1,'sasgf');*/

30         /*  put have=;*/

31           put 'want=saasggff';

32         have=prxchange('s/(a|g|f){1}?/$1$1/',-1,'sasgf');

33         put have=;

34         run;

 

want=saasggff

have=saasggff

          

36         data _null_;

37         /*have=prxchange('s/a|g|f/$1xx$2yy$3zz/',-1,'sasgf');*/

38         /*put have=;*/

39         put 'want=sxxsyyzz';

40         have=prxchange('s/([^agf]*)(a){1}?([^agf]*)(g){1}?([^agf]*)(f){1}?/$1xx$3yy$5zz/',-1,'sasgf');

41         put have=;

42         run;

 

want=sxxsyyzz

have=sxxsyyzz

 

The former prxchange works, but only if you want to duplicate specific characters in an input string.

But I guess that's not exactly what you were after... The latter prxchange should be closer to meet your requirements. 

So the $64k question is: does it?

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
  • 8 replies
  • 2634 views
  • 5 likes
  • 3 in conversation