BookmarkSubscribeRSS Feed
curious_tad
Calcite | Level 5

Hello everybody,

 

fairly new in the world of SAS, a curious person,say, learning in his own time. 

 

I am trying to riddle out how to make a successful POST/GET request to REST API using %macro and JSON as the primary payload and then set up a table that tracks a history of past requests and responses. Set this task to myself as it's one of the more common things people aim to do in other languages. Working with APIs. While, it takes me about 10 minutes to write such logic in JS or Python without any headaches, though I cannot string up a working logic in SAS using %macro (a term function could also be used as a synonym I guess?). 

 

A non-macro approach clicked using datalines and a hard-coded value as a prompt. I am now well aware that datalines will not budge when using macro logic so I went with put, but still not a single budge. 

 

Here's the code that I have managed to jot down so far. The API is crudcrud.com allowing up to 100 free requests.

 

%macro api_post_request(prompt=);
      %let api_url=url;
      filename json_in temp;
      data _null_;
            file in;
            put "{
             ""username"":""&prompt""
       }"
       ;
      run;

      filename resp temp;

      proc http method="POST"
                  url="&api_url
                  ct="application/json"
                  in=json_in
                  out=resp;
            headers
            "Content-Type"="application/json";
      run;
%mend;

%api_post_request(prompt=Player1);

Fierce thanks!

3 REPLIES 3
Patrick
Opal | Level 21

Before using any macro language to make things dynamic one should always first get some static code working.

 

You need first below to work. I would for the first iteration even replace the macro variables with hard coded strings in your code. 

  %let api_url=url;
  %let prompt=Player1;
  filename json_in temp;
  data _null_;
    file in;
    put "{
     ""username"":""&prompt""
   }"
   ;
  run;

  filename resp temp;

  proc http method="POST"
      url="&api_url
      ct="application/json"
      in=json_in
      out=resp;
    headers
    "Content-Type"="application/json";
  run;

Very first issue are unbalanced quotation marks

      url="&api_url
      ct="application/json"

Also ensure that there aren't any problematic characters in your URL. Ideally use SAS function urlencode() to create a url that won't cause issues.

LaurieF
Barite | Level 11

I do this quite a lot, pulling data from Genesys Cloud in automated processes. It took me ages to get my ducks in a row; one of the major tools I used was examining the return code.

 

After your call, check &sys_prochttp_status_code. If it's 200 or 202, you're getting close. Anything else and you'll get an indication of where your problem lies - see &sys_prochttp_status_phrase. Just an indication, mind. It's tricky.

ChrisHemedinger
Community Manager

A couple of webinars to help your learning:

- How to use PROC HTTP and the JSON engine

- How to use JSON data in SAS

 

I second the suggestion from others that it is best to get your process working in straight code before jumping to macro language. People coming from other programming languages are often tempted by macro language as they see that as the way to emulate a process they know from other systems, but SAS macro is mainly useful to add data-driven iteration and logic to an already-working set of code steps.

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1105 views
  • 1 like
  • 4 in conversation