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.
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;
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;
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.