BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
shlomiohana
Obsidian | Level 7
Hi,
How can i convert XML to URL with sas?

Example:
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<SMF_REQUEST_PARAMS>
<customerIdType>1</customerIdType>
<customerId>312755468</customerId>
</SMF_REQUEST_PARAMS>

URL CONVERT:
%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%0A%3CSMF_REQUEST_PARAMS%3E%0A%0A%20%20%20%3CcustomerIdType%3E1%3C%2FcustomerIdType%3E%0A%0A%20%20%20%3CcustomerId%3E312755468%3C%2FcustomerId%3E%0A%0A%3C%2FSMF_REQUEST_PARAMS%3E

Thanks.
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
have='<?xml version="1.0" encoding="UTF-8"?>
<SMF_REQUEST_PARAMS>
<customerIdType>1</customerIdType>
<customerId>312755468</customerId>
</SMF_REQUEST_PARAMS>';
want=urlencode(have);
run;

Ksharp_0-1689250035854.png

 

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @shlomiohana,

 

You can use the URLENCODE function:

data have;
input xml $80.;
cards;
<?xml version="1.0" encoding="UTF-8"?>
<SMF_REQUEST_PARAMS>
<customerIdType>1</customerIdType>
<customerId>312755468</customerId>
</SMF_REQUEST_PARAMS>
;

data want;
set have end=last;
length url $1000;
url=catt(url,urlencode(trim(xml)));
if last;
retain url;
run;

If you really need the double linefeed characters (hex 0A) representing the line breaks, insert

if not last then url=catt(url,'%0A%0A');

before the subsetting IF statement if last.

 

Your sample output also shows some blanks (hex 20) after line 2 and 3. To reproduce this in this particular example, you could insert a second IF statement:

if _n_ in (2, 3) then url=catt(url,'%20%20%20');

but I don't think this is useful.

Ksharp
Super User
data have;
have='<?xml version="1.0" encoding="UTF-8"?>
<SMF_REQUEST_PARAMS>
<customerIdType>1</customerIdType>
<customerId>312755468</customerId>
</SMF_REQUEST_PARAMS>';
want=urlencode(have);
run;

Ksharp_0-1689250035854.png

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1267 views
  • 0 likes
  • 3 in conversation