Hi,
I have two datasets:
* have1, a list of non-standardized codes
* have2, a list of regular expressions and a standardized code
For each record in have1, I want to search for a regular expression in have2 that matches the code. If there is a matching regular expression, I want to put the value of the standardized code
The code below achieves the desired output, but it has the patterns hard coded. In other word, it doesnt use have2 at all. I want to change the code in the last data step so the patterns are read from have2. In this manner, the pattern list can be dynamic as have2 changes. The same code is also attached.
Do you know if this can be achieved?
Thanks a lot!
DATA have1;
INPUT
code : $CHAR255. ;
DATALINES4;
Acreditacion por rechazo de transferencia
Acreditacion por rechazo de transferencia (cuenta cerrada)
Ajuste por error de acreditacion. De 4743 a 21606
Ajuste por error de Imputaci¢n (Rc Nø89162)
ARPY020700006
ARPY220300023
ARPY230800024
ARPY240800031
;;;;
DATA have2;
INFILE DATALINES4
DLM='09'X
MISSOVER
DSD ;
INPUT
regexp_id : BEST32.
regexp : $CHAR255.
replacement : $CHAR255. ;
DATALINES4;
1 /acreditacion/ Acreditación por rechazo transferencia
2 /^ajuste[\w\s]/ Ajuste por error de acreditación entre cuentas
3 /py\d{9}/ Ingreso CPD
4 /comision*SGR$/ Comisión de terceros - SGR
;;;;
data want;
set have1;
IF _N_ = 1 THEN DO;
patt1 = prxparse ("/acreditacion/");
patt2 = prxparse ("/^ajuste[\w\s]/");
patt3 = prxparse ("/py\d{9}/");
patt4 = prxparse ("/comision*SGR$/");
END;
RETAIN patt1-patt4;
code = strip(lowcase(code));
IF ( prxmatch(patt1, code) > 0) THEN std_code = "Acreditación por rechazo transferencia";
IF ( prxmatch(patt2, code) > 0) THEN std_code = "Ajuste por error de acreditación";
IF ( prxmatch(patt3, code) > 0) THEN std_code = "Ingreso CPD";
IF ( prxmatch(patt4, code) > 0) THEN std_code = "Comisión de terceros - SGR";
DROP patt1-patt4;
run;
1. The data -generation code you provided does not work.
I fixed it. Please try to provide working code, and test it.
2. I made the regular expressions case insensitive to get good matches. Change as needed.
3. This works:
data HAVE1;
input CODE & $char255. ;
datalines4;
Acreditacion por rechazo de transferencia
Acreditacion por rechazo de transferencia (cuenta cerrada)
Ajuste por error de acreditacion. De 4743 a 21606
Ajuste por error de Imputaci¢n (Rc Nø89162)
ARPY020700006
ARPY220300023
ARPY230800024
ARPY240800031
;;;;
run;
data HAVE2;
input REGEXP_ID
REGEXP : $char255.
REPLACEMENT & $char255. ;
datalines4;
1 /acreditacion/i Acreditación por rechazo transferencia
2 /^ajuste[\w\s]/i Ajuste por error de acreditación entre cuentas
3 /py\d{9}/ Ingreso CPD
4 /comision*SGR$/ Comisión de terceros - SGR
;;;;
run;
proc sql;
select *
from HAVE1, HAVE2
where prxmatch(REGEXP,CODE);
quit;
CODE | REGEXP_ID | REGEXP | REPLACEMENT |
---|---|---|---|
Acreditacion por rechazo de transferencia | 1 | /acreditacion/i | Acreditación por rechazo transferencia |
Acreditacion por rechazo de transferencia (cuenta cerrada) | 1 | /acreditacion/i | Acreditación por rechazo transferencia |
Ajuste por error de acreditacion. De 4743 a 21606 | 1 | /acreditacion/i | Acreditación por rechazo transferencia |
Ajuste por error de acreditacion. De 4743 a 21606 | 2 | /^ajuste[\w\s]/i | Ajuste por error de acreditación entre cuentas |
Ajuste por error de Imputaci¢n (Rc Nø89162) | 2 | /^ajuste[\w\s]/i | Ajuste por error de acreditación entre cuentas |
1. The data -generation code you provided does not work.
I fixed it. Please try to provide working code, and test it.
2. I made the regular expressions case insensitive to get good matches. Change as needed.
3. This works:
data HAVE1;
input CODE & $char255. ;
datalines4;
Acreditacion por rechazo de transferencia
Acreditacion por rechazo de transferencia (cuenta cerrada)
Ajuste por error de acreditacion. De 4743 a 21606
Ajuste por error de Imputaci¢n (Rc Nø89162)
ARPY020700006
ARPY220300023
ARPY230800024
ARPY240800031
;;;;
run;
data HAVE2;
input REGEXP_ID
REGEXP : $char255.
REPLACEMENT & $char255. ;
datalines4;
1 /acreditacion/i Acreditación por rechazo transferencia
2 /^ajuste[\w\s]/i Ajuste por error de acreditación entre cuentas
3 /py\d{9}/ Ingreso CPD
4 /comision*SGR$/ Comisión de terceros - SGR
;;;;
run;
proc sql;
select *
from HAVE1, HAVE2
where prxmatch(REGEXP,CODE);
quit;
CODE | REGEXP_ID | REGEXP | REPLACEMENT |
---|---|---|---|
Acreditacion por rechazo de transferencia | 1 | /acreditacion/i | Acreditación por rechazo transferencia |
Acreditacion por rechazo de transferencia (cuenta cerrada) | 1 | /acreditacion/i | Acreditación por rechazo transferencia |
Ajuste por error de acreditacion. De 4743 a 21606 | 1 | /acreditacion/i | Acreditación por rechazo transferencia |
Ajuste por error de acreditacion. De 4743 a 21606 | 2 | /^ajuste[\w\s]/i | Ajuste por error de acreditación entre cuentas |
Ajuste por error de Imputaci¢n (Rc Nø89162) | 2 | /^ajuste[\w\s]/i | Ajuste por error de acreditación entre cuentas |
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.