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

I have an issue where a variable has 

var1

1-Text out of range

11-minimum-score of text

 

and 

var2

MES1102

MES1129

 

I want to replace after "-" from var1 and get text in var2 in that place. something like below.

 

var3

MES1102-Text out of range

MES1129-minimum-score of text

 

any help on how to do it in datastep

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @noda6003 

 

Here is one approach to achieve this:

data have;
	infile datalines dlm=",";
	input var1:$50. var2:$50.;
	datalines;
1-Text out of range,MES1102
11-minimum-score of text,MES1129
	;
run;

data want;
	set have;
	length var3 $ 50;
	var3 = tranwrd(strip(var1),scan(var1,1,"-"),strip(var2));
run;

Output:

Capture d’écran 2020-05-03 à 10.38.50.png

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

Hi @noda6003 

 

Here is one approach to achieve this:

data have;
	infile datalines dlm=",";
	input var1:$50. var2:$50.;
	datalines;
1-Text out of range,MES1102
11-minimum-score of text,MES1129
	;
run;

data want;
	set have;
	length var3 $ 50;
	var3 = tranwrd(strip(var1),scan(var1,1,"-"),strip(var2));
run;

Output:

Capture d’écran 2020-05-03 à 10.38.50.png

ed_sas_member
Meteorite | Level 14

Another possibility could be:

data want;
	set have;
	length var3 $ 50;
	var3 = catx("-",var2,prxchange('s/\d+-(.*)/$1/',1,var1));
run;

-> it concatenates var2 and the second part of var1 with the delimiter "-"

Shmuel
Garnet | Level 18

If there is only one '-' in var1 you can use next code:

langth var3 $30; /* adapt to max length expected */
var3 = cats(var2, scan(var1,2,'-'));

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore 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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1895 views
  • 1 like
  • 3 in conversation