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

When there are multiple ampersands in one double-quoted string, can one resolve only some of them leaving others unresolved? For example,

%macro repeat;

%do pagenumber=1 %to 5;

filename tempfile temp;

proc http method="get" out=tempfile
	url="https://www.walmart.com/search/?cat_id=0&page=&pagenumber.&ps=40&query=chobani#searchProductResult";
run;

data output&pagenumber.;
	infile tempfile length=length lrecl=32767;
	input line $varying32767. length;
run;

%end;

%mend;

%repeat;

Since PROC HTTP requires a string for URL, I put a double-quoted string, which resolves all ampersands. If there is a macro variable such as QUERY, then SAS resolves it as the string has &QUERY. I need to only resolve PAGENUMBER since iterate, but don't want to resolve anything else. How can I distinguish those different ampersands? I tried to combine a single-quoted string and a double-quoted one by ||, but didn't work well. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why not build up the string from the pieces and use the QUOTE() function to enclose it in single quotes.

%macro repeat;
%local pagenumber url ;
%do pagenumber=1 %to 5;
  filename tempfile temp;

  data _null_;
    call symputx(url,quote(cats
      ('https://www.walmart.com/search/?cat_id=0&page='
      ,"&pagenumber"
      ,'&ps=40&query=chobani#searchProductResult'
      ),"'"))
    ;
  run;

  proc http method="get" out=tempfile url=&url ;
  run;

  data output&pagenumber.;
    infile tempfile truncover ;
    input line $char32767.;
  run;

%end;

%mend;

%repeat;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Why not build up the string from the pieces and use the QUOTE() function to enclose it in single quotes.

%macro repeat;
%local pagenumber url ;
%do pagenumber=1 %to 5;
  filename tempfile temp;

  data _null_;
    call symputx(url,quote(cats
      ('https://www.walmart.com/search/?cat_id=0&page='
      ,"&pagenumber"
      ,'&ps=40&query=chobani#searchProductResult'
      ),"'"))
    ;
  run;

  proc http method="get" out=tempfile url=&url ;
  run;

  data output&pagenumber.;
    infile tempfile truncover ;
    input line $char32767.;
  run;

%end;

%mend;

%repeat;
Junyong
Pyrite | Level 9
Thanks. I used something similar with %LET and %NRSTR, but wondered whether one can shorten this by 'https://www.walmart.com/search/?cat_id=0&page='||"&pagenumber"||'&ps=40&query=chobani#searchProductR...' or something like \&.
Tom
Super User Tom
Super User
%let x=%str(&)page;

Junyong
Pyrite | Level 9

Much appreciate. This was what I thought.

%macro repeat;

%do pagenumber=1 %to 5;

filename tempfile temp;

proc http method="get" out=tempfile
	url="https://www.walmart.com/search/?cat_id=0%str(&)page=&pagenumber.%str(&)ps=40%str(&)query=chobani#searchProductResult";
run;

data output&pagenumber.;
	infile tempfile length=length lrecl=32767;
	input line $varying32767. length;
run;

%end;

%mend;

%repeat;

Thanks.

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
  • 4 replies
  • 1186 views
  • 1 like
  • 2 in conversation