extraction chaine de caracteres

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

extraction chaine de caracteres

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


Accepted Solutions
Solution
‎02-03-2017 04:04 AM
PROC Star
Posts: 7,432

Re: extraction chaine de caracteres

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

 

View solution in original post


All Replies
Solution
‎02-03-2017 04:04 AM
PROC Star
Posts: 7,432

Re: extraction chaine de caracteres

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

 

Occasional Contributor
Posts: 16

Re: extraction chaine de caracteres

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

New Contributor
Posts: 2

Re: extraction chaine de caracteres

Thank you ! It works perfectly for me
Respected Advisor
Posts: 4,812

Re: extraction chaine de caracteres

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;
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 186 views
  • 3 likes
  • 4 in conversation