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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

4 REPLIES 4
art297
Opal | Level 21

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

 

mansour_ibrahim
Obsidian | Level 7

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

roll4life
Fluorite | Level 6
Thank you ! It works perfectly for me
PGStats
Opal | Level 21

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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register 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
  • 4 replies
  • 4135 views
  • 3 likes
  • 4 in conversation