BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

Hi Everyone,

 

Could you please suggest how to extract Following highlighted part only. This abcdxy will always between code= and &session_state

 

login.microsoftonline.com/common/oauth2code=abcdxy&session_state=ab123bf2345
 
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data demo;
  have='login.microsoftonline.com/common/oauth2code=abcdxy&session_state=ab123bf2345';
  length want $20;
  s=find(have,'code=');
  e=find(have,'&session_state');
  want=substr(have,s+5,e-s-5);
run;

View solution in original post

6 REPLIES 6
Patrick
Opal | Level 21
data demo;
  have='login.microsoftonline.com/common/oauth2code=abcdxy&session_state=ab123bf2345';
  length want $20;
  want=scan(have,2,'=&');
run;
proc print data=demo;
run;

Patrick_0-1669704583192.png

 

s_lassen
Meteorite | Level 14

The solution suggested by @Patrick only works if the "code=" is the first part after the actual net address. If that is not the case, I would suggest something like this:

 

data have; /* example data */
  url='login.microsoftonline.com/common/oauth2code=abcdxy&session_state=ab123bf2345';
run;

data want;
  set have;
  pos=prxmatch('/(?<=code\=)[[:alnum:]]+(?=&session_state)/',url);
  if pos then 
    code=scan(substr(url,pos),1,'&');
run;

The PRX expression looks for an alphanumeric string between "code=" and "&session_state". If it is found, we can then use SCAN to find the code. If your code can contain other characters that alphanumerics, take a look at the PRX documentation or just replace "[[:alnum:]]" with "." - the general wildcard.

 

kumarsandip975
Pyrite | Level 9
@s_lassen thanks. I tested with your suggestion, it is working fine with sort length value between code= and &session_state, but I have approx 700+ charater string between these two keywords. Can you please suggest, how to deal with long characters values.
s_lassen
Meteorite | Level 14

Is it the code itself that is more than 700 characters? or is it that you have other HTML parameters between the code and the session_state?

 

In the first case, I do not see why you should not get the correct code.

In the second case, where you have e.g.

'login.microsoftonline.com/common/oauth2code=abcdxy&name=george&session_state=ab123bf2345';

you should try changing the PRX call to

pos=prxmatch('/(?<=code\=)[[:alnum:]]+(?=&)/',url);

- this will find any instance of code=[alphanumeric value], followed by another HTML parameter.

 

Patrick
Opal | Level 21

You need to create the variable with an appropriate length instead of SAS let it do implicitly. 

 

To create variable code with a length of 700

Patrick_1-1669841735886.png

 

Or to create variable code with the same length as variable url which should always be sufficient:

Patrick_2-1669841770783.png

 

 

 

Ksharp
Super User
data demo;
  have='login.microsoftonline.com/common/oauth2code=abcdxy&session_state=ab123bf2345';
  length want $20;
  s=find(have,'code=');
  e=find(have,'&session_state');
  want=substr(have,s+5,e-s-5);
run;
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
  • 6 replies
  • 1422 views
  • 0 likes
  • 4 in conversation