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
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:
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:
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 "-"
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,'-'));
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.