BookmarkSubscribeRSS Feed
Hoibai
Obsidian | Level 7

I have this string: "Cdtr":{"Nm":"DEUTSCHE BANK"}

How can I implement this in SAS like the function

regexp_extract(fulltxinf_intx, '"Cdtr":."Nm":"([^"]+)', 1) as String_Ort

 

Thanks

14 REPLIES 14
PaigeMiller
Diamond | Level 26

@Hoibai 

Please tell us what this regexp_extract function does.

--
Paige Miller
Hoibai
Obsidian | Level 7

The function returns me the result DEUTSCHE BANKthat the parameters Cdtr":{"Nm": are in front of it.

 

My string: "Cdtr":{"Nm":"DEUTSCHE BANK"}

regexp_extract(string, '"Cdtr":."Nm":"([^"]+)', 1) as String_Ort

 

Quentin
Super User

Can you give a few more examples of strings you would like to parse?  

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
ChrisNZ
Tourmaline | Level 20

Like this?

data T;
  STR1 = '"Cdtr":{"Nm":"DEUTSCHE BANK"}';
  STR2 = prxchange('s/"Cdtr":."Nm":"([^"]+).*/\1/', 1, STR1);
  put STR2=;
run;
STR2=DEUTSCHE BANK
ChrisNZ
Tourmaline | Level 20

Not too sure why you have

  STR2 = prxchange('s/"Cdtr":."Nm":"([^"]+).*/\1/', 1, STR1);

I'd replace the . with a {

  STR2 = prxchange('s/"Cdtr":{"Nm":"([^"]+).*/\1/', 1, STR1);

 

 

Hoibai
Obsidian | Level 7

The problem is, the first record is with {"Cdtr":{"Nm":"DEUTSCHE BANK"} .

The second record  is with '{"Cdtr":{"Nm":"VERGOELST GMBH"}'

according to the constant variables {"Cdtr":{"Nm":, each record in the file has a different value.

ChrisNZ
Tourmaline | Level 20
That's why I use a variable name. Whatever is in STR1 will be read into STR2.
Swap with the names of your variables.
Hoibai
Obsidian | Level 7

my string is 300 - 400 Bytes long.

{"Cdtr":{"Nm":"DEUTSCHE BANK"},"CdtrAcct":{"Id":{"IBAN":"DE0111111111111111111100"}},

ChrisNZ
Tourmaline | Level 20
The parsing should still work. Have you tried?
Hoibai
Obsidian | Level 7

my string begins

{"ar:pacs.003.002.04":{"Cdtr":{"Nm":"DEUTSCHE BANK"},"CdtrAcct":{"Id":{"IBAN":"DE0111111111111111111100"}}

 

that's why I get this result: {"ar:pacs.003.002.04":{DEUTSCHE BANK

 

Quentin
Super User

Can you please give a few examples (just 5-10) of the full string you are trying to parse?  It will help people help you.

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
ChrisNZ
Tourmaline | Level 20

that's why I get this result: {"ar:pacs.003.002.04":{DEUTSCHE BANK

 

data HAVE;
  input STR1 &:$300.;
cards;  
{"ar:pacs.003.002.04":{"Cdtr":{"Nm":"DEUTSCHE BANK"},"CdtrAcct":{"Id":{"IBAN":"DE0111111111111111111100"}}
{"Cdtr":{"Nm":"DEUTSCHE BANKX"},"CdtrAcct":{"Id":{"IBAN":"DE0111111111111111111100"}}
{"ar:pacs.003.002.04":{"Cxxx":{"Nm":"DEUTSCHE BANKY"},"CdtrAcct":{"Id":{"IBAN":"DE0111111111111111111100"}}
run;

proc sql; 
  select ifc( prxchange('s/.*"Cdtr":{"Nm":"([^"]+).*/\1/', 1, STR1) = STR1, ' ', prxchange('s/.*"Cdtr":{"Nm":"([^"]+).*/\1/', 1, STR1) ) as STRING_ORT
  from HAVE;
quit;  

 

I cater for cases when there is not matching pattern with the ifc() test.

 

STRING_ORT
DEUTSCHE BANK
DEUTSCHE BANKX
 

 

 

 

 

Tom
Super User Tom
Super User

@Hoibai wrote:

The problem is, the first record is with {"Cdtr":{"Nm":"DEUTSCHE BANK"} .

The second record  is with '{"Cdtr":{"Nm":"VERGOELST GMBH"}'

according to the constant variables {"Cdtr":{"Nm":, each record in the file has a different value.


 

I don't understand what you mean be this.  Of course each observation has a different value.  But the PREFIX you are searching for looks exactly the same in both of those example values.  

 

{"Cdtr":{"Nm":"

Personally I avoid having to use regex if I can get the result I want with simpler functions.

 

 

data have;
  infile cards truncover;
  input str $200.;
cards4;
{"Cdtr":{"Nm":"DEUTSCHE BANK"}
{"Cdtr":{"Nm":"VERGOELST GMBH"}
{"ar:pacs.003.002.04":{"Cdtr":{"Nm":"DEUTSCHE BANK"},"CdtrAcct":{"Id":{"IBAN":"DE0111111111111111111100"}}
;;;;

data want;
  length cdtr_nm $60 ;
  set have;
  sp=index(str,'{"Cdtr":{"Nm":');
  if sp then cdtr_nm = dequote(substr(str,sp+length('{"Cdtr":{"Nm":')));
run;

Tom_1-1722261078083.png

 

 

 

mkeintz
PROC Star

Do they all have the form 

   "Cdtr":{"Nm":"XXX XXXX XXXXXX"}

where "XXX XXXX XXXXXX" is the desired result, and can have variable length?

 

If so, you can always skip the first 14 characters and drop the last 2 characters, which can be done with the SUBSTR and LENGTH functions:

 

data sample;
  string='"Cdtr":{"Nm":"DEUTSCHE BANK"}';
  new_string=substr(string,15,length(string)-16);

  put (_all_) (= /);
run;

Or if you want something more general, perhaps you can characterize the desired substring as the 5th word, where each double quote is treated as a word separator.  Then, as long as the desired expression is double-quoted, and it is preceded by two expressions that are also double quoted (i.e. preceded by 4 double quotes), you can do this: 

data sample;
  string='"Cdtr":{"Nm":"DEUTSCHE BANK"}';
  new_string=scan(string,5,'"');
  put (_all_) (= /);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 363 views
  • 2 likes
  • 6 in conversation