Bonsoir,
Je débutes avec SAS ET J'aimerais extraire une partie d'une chaine de caractères qui se présente dans un tableau comme suit
Col1 Col2 Col3
1 bismuth WZ2801201714515390972268</cp_check_id><result>0</result><vart_id>01.269821<
2 Gandhi <cp_reply><cp_id>TOPCPID</cd><cp_tr_id><result>21</result>credit_bal><end_val_daten
3 FILLON 01010CPUSSDGWID02024WZ2801201718562191813204050101485629752210010> <result>15</result>801244522584256219181
4 PENELOPE 4WZ2801<result>5</result><2017<end_val_date>19/07/2025</end_val_date><end_inact_date>18/01/2038<
la longueur de la Col3 est variante, je veux donc extraire dans une autre table les Col1 Col2et le résultat de la Col3 qui est toujours délimité par <result>12</result>
Merci à tous pour vos réponses.
Cordialement
There might be a more direct route, but the following will work:
data have; informat Col2 $8.; informat Col3 $char80.; input Col1 Col2 Col3 &; cards; 1 bismuth WZ2801201714515390972268 </ cp_check_id> <result> 0 </ result> <vart_id> 01.269821 < 2 Gandhi <cp_reply> <cp_id> TOPCPID </ cd> <cp_tr_id> <result> 21 </ result> credit_bal> <end_val_daten 3 FILLON 01010CPUSSDGWID02024WZ2801201718562191813204050101485629752210010> <result> 15 </ result> 801244522584256219181 4 PENELOPE 4WZ2801 <result> 5 </ result> <2017 <end_val_date> 19/07/2025 </ end_val_date> <end_inact_date> 18/01/2038 < ; data want; set have; result=input(scan(substr(col3,find(col3,'<result>')+8),1,'<'),8.); run;
HTH,
Art, CEO, AnalystFinder.com
There might be a more direct route, but the following will work:
data have; informat Col2 $8.; informat Col3 $char80.; input Col1 Col2 Col3 &; cards; 1 bismuth WZ2801201714515390972268 </ cp_check_id> <result> 0 </ result> <vart_id> 01.269821 < 2 Gandhi <cp_reply> <cp_id> TOPCPID </ cd> <cp_tr_id> <result> 21 </ result> credit_bal> <end_val_daten 3 FILLON 01010CPUSSDGWID02024WZ2801201718562191813204050101485629752210010> <result> 15 </ result> 801244522584256219181 4 PENELOPE 4WZ2801 <result> 5 </ result> <2017 <end_val_date> 19/07/2025 </ end_val_date> <end_inact_date> 18/01/2038 < ; data want; set have; result=input(scan(substr(col3,find(col3,'<result>')+8),1,'<'),8.); run;
HTH,
Art, CEO, AnalystFinder.com
Bonjour,
Je te propose une autre solution:
data have;
informat Col2 $8.;
informat Col3 $char80.;
input Col1 Col2 Col3 &;
cards;
1 bismuth WZ2801201714515390972268 </ cp_check_id> <result> 0 </ result> <vart_id> 01.269821 <
2 Gandhi <cp_reply> <cp_id> TOPCPID </ cd> <cp_tr_id> <result> 21 </ result> credit_bal> <end_val_daten
3 FILLON 01010CPUSSDGWID02024WZ2801201718562191813204050101485629752210010> <result> 15 </ result> 801244522584256219181
4 PENELOPE 4WZ2801 <result> 5</ result> <2017 <end_val_date> 19/07/2025 </ end_val_date> <end_inact_date> 18/01/2038 <
;
DATA test1(keep=col: resultat);
set have;
if _N_=1 then RE = PRXPARSE("/\d+ ?\<\/ result\>/");
retain RE ;
if PRXMATCH(RE,col3) > 0 then do;
call PRXSUBSTR(RE,col3,START,LENGTH);
resultat = SUBSTRN(col3,START ,LENGTH);
RX=PRXPARSE("s/\<\/ result\>/ /");
CALL PRXCHANGE(RX,-1,resultat);
end;
run;
Cordialement
Ou plus simplement :
data want;
if not prxId then prxId + prxParse("#<result>(.*)</result>#i");
set have;
if prxMatch(prxId, col3) then
valeur = input(prxPosn(prxId, 1, col3), best.);
run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.