Try to fetch data from a secured website (ID and password) using the macro
%macro http(http=%nrstr(), save_file=, hostname=, delimiter=%str(|),fileref=browser, termstr=CRLF);
%local str1 str2 pos http;
%let str1=;
%let str2=;
%do %while (%index(&http,&delimiter)>0);
%let pos=%index(&http,&delimiter);
%let str1=&str1%qsubstr(&http, 1, &pos-1)/;
%let str2=&str2%qsubstr(&http, 1, &pos-1)'0d0a'x;
%let http=%qsubstr(&http, &pos+1, %length(&http)-&pos);
%end;
filename &fileref socket "&hostname:80" termstr=&termstr lrecl=8192;
data _null_;
retain chunked;
length rec2 $10000 chunked $1;
infile &fileref length=len;
if _n_=1 then do;
file &fileref;
put %unquote(&str1);
file &save_file;
put '>>> HTTP Request:' '0d0a'x;
put %unquote(&str2);
put '0d0a'x '<<< HTTP Response:' '0d0a'x;
end;
file &save_file recfm=n;
input record $varying8192. len;
put record $varying8192. len '0d0a'x;
if record='Transfer-Encoding: chunked' then chunked='Y';
if len=0 and chunked='Y' then do while (1); %*** look for & process chunks;
input chunksize $varying8. len / record $varying8192. len;
if len ne 0 then do;
l2=len+countc(record, '0a'x);
rec2=tranwrd(record, '0a'x, '0d0a'x);
put rec2 $varying10000. l2;
end;
else stop; *** found null record at end of file, end loop;
end;
run;
%mend;
in the SAS document:
http://www2.sas.com/proceedings/sugi30/100-30.pdf
but got the error:
>>> HTTP Request:
POST /accounts/login/?next=/admin/epr/toothtreatmentordering/1980/history/ HTTP/1.1
Host: eprctr.dartmouth.edu
Connection: close
Cookie: sessionid=2d4cfc55f32e950d30c6aa5008693b99;
Content-Length: 47
j_username=myname&j_password&Logon=Log+On
<<< HTTP Response:
HTTP/1.1 400 Bad Request
Date: Wed, 01 Oct 2014 13:46:03 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 413
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
Request header field is missing ':' separator.<br />
<pre>
j_username=myname&j_password&Logon=Log+On</pre>
</p>
<hr>
<address>Apache/2.2.14 (Ubuntu) Server at localhost Port 80</address>
</body></html>
Your suggestions will be appreciated!
Andrew
Not sure . Did you try proc http ?
Yes, I did. I have username and password. But it returns with empty out put.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.