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

Hello,


I try to pass a global variable in the urlencode function and get an invalid link, I run the following code:

%let globalVar = 201080;

data  test;
    original_string = urlencode('<SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>');
run;

If I enter the number itself 201080 I will get a proper link.

my question is how to pass a global variable to the urlencode function?

 

I get a valid link:

urlencode('<SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>201080</SMF_RETURN_MESSAGE></SMF_REPLY>')

 

thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Below code should do as long as you don't expect an ampersand or percent sign in your source string that shouldn't get interpreted as a macro token.

%let globalVar = 201080;

data test;
  length original_string $300;
  original_string = 
    '<?xml version="1.0" encoding="UTF-8"?><SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>';
  original_string=urlencode(strip(resolve(original_string)));
  put original_string;
run;

The encoded string will be longer than the original string. Make sure that your variable has a length defined that's sufficient for the encoded string.

 

Or this way will also work

data test;
  length original_string $300;
  original_string = cats(
    '<?xml version="1.0" encoding="UTF-8"?><SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>'
    ,"&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>"
    );
  original_string=urlencode(strip(original_string));
  put original_string;
run;

 

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

You need to use double quotes for the SAS macro variable to resolve.

Patrick_0-1700517554334.png

%let globalVar = 201080;

data  test;
    original_string = urlencode("<SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>");
run;

 

shlomiohana
Obsidian | Level 7
This is not good i get an error because the full urlencode is:

original_string = urlencode('<?xml version="1.0" encoding="UTF-8"?><SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>'
Patrick
Opal | Level 21

Below code should do as long as you don't expect an ampersand or percent sign in your source string that shouldn't get interpreted as a macro token.

%let globalVar = 201080;

data test;
  length original_string $300;
  original_string = 
    '<?xml version="1.0" encoding="UTF-8"?><SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>';
  original_string=urlencode(strip(resolve(original_string)));
  put original_string;
run;

The encoded string will be longer than the original string. Make sure that your variable has a length defined that's sufficient for the encoded string.

 

Or this way will also work

data test;
  length original_string $300;
  original_string = cats(
    '<?xml version="1.0" encoding="UTF-8"?><SMF_REPLY><SMF_REQUEST_ID>aaa</SMF_REQUEST_ID><SMF_RETURN_CODE>1</SMF_RETURN_CODE><SMF_RETURN_MESSAGE>'
    ,"&globalVar</SMF_RETURN_MESSAGE></SMF_REPLY>"
    );
  original_string=urlencode(strip(original_string));
  put original_string;
run;

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 228 views
  • 0 likes
  • 2 in conversation