%let CONSUMER_KEY=your key; %let CONSUME_SECRET=your secret; %let JSON_TWEET_FILE=W:\SAS\TwitterSAS\ResponseContent.txt; %let CSV_TWEET_FILE=W:\SAS\TwitterSAS\ResponseContent.csv; %let TWEET_QUERY=shaq+OR+kingjames; /* Create a temporary file name used for the XMLMap */ %macro tempFile( fname ); %if %superq(SYSSCPL) eq %str(z/OS) or %superq(SYSSCPL) eq %str(OS/390) %then %let recfm=recfm=vb; %else %let recfm=; filename &fname TEMP lrecl=2048 &recfm; %mend; /* create temp files for the content and header input streams */ %tempFile(in); %tempFile(hdrin); /* keep the response permanently */ filename out "&JSON_TWEET_FILE."; /* post request content is the grant_type */ data _null_; file in; put "grant_type=client_credentials&"; run; /* request the bearer token by providing consumer key and secret */ data _null_; file hdrin; consumerKey = urlencode("&CONSUMER_KEY."); consumerSecret = urlencode("&CONSUME_SECRET."); encodedAccessToken = put( compress(consumerKey || ":" || consumerSecret),$base64x32767.); put "Authorization: Basic " encodedAccessToken; run; proc http method="post" in=in out=out headerin=hdrin url="https://api.twitter.com/oauth2/token" ct="application/x-www-form-urlencoded;charset=UTF-8"; run; /* Store bearer token in macro variable 'BEARER_TOKEN' */ proc groovy classpath="W:\SAS\TwitterSAS\groovy-all-2.4.12.jar"; submit "&JSON_TWEET_FILE."; import groovy.json.JsonSlurper def input = new File(args[0]).text def result = new JsonSlurper().parseText(input) println "Recieved bearer token: ${result.access_token}" exports.putAt('BEARER_TOKEN',result.access_token) endsubmit; quit; /* Use the bearer token as authorization */ data _null_; file hdrin; put "Authorization: Bearer &BEARER_TOKEN."; run; /* Search for tweets; Limit output to 100 tweets */ proc http method="get" out=out headerin=hdrin url="https://api.twitter.com/1.1/search/tweets.json?q=&TWEET_QUERY.&count=50" ct="application/x-www-form-urlencoded;charset=UTF-8"; run; /* parse JSON and generate temporary CSV file */ proc groovy classpath="W:\SAS\TwitterSAS\groovy-all-2.4.12.jar;W:\SAS\TwitterSAS\opencsv-3.10.jar"; submit "&JSON_TWEET_FILE." "&CSV_TWEET_FILE."; import groovy.json.* import com.opencsv.CSVWriter def input = new File(args[0]).text def output = new JsonSlurper().parseText(input) def csvoutput = new FileWriter(args[1]) CSVWriter writer = new CSVWriter(csvoutput); String[] header = new String[15]; header[0] = "id"; header[1] = "text"; header[2] = "truncated"; header[3] = "created_at"; header[4] = "user.id"; header[5] = "user.name"; header[6] = "user.screen_name"; header[7] = "user.location"; header[8] = "user.description"; header[9] = "user.url"; header[10] = "user.followers_count"; header[11] = "user.friends_count"; header[12] = "user.listed_count"; header[13] = "retweet_count"; header[14] = "favorite_count"; writer.writeNext(header); output.statuses.each { String[] content = new String[15]; content[0] = it.id.toString(); content[1] = it.text.toString(); content[2] = it.truncated.toString(); content[3] = it.created_at.toString(); content[4] = it.user.id.toString(); content[5] = it.user.name.toString(); content[6] = it.user.screen_name.toString(); content[7] = it.user.location.toString(); content[8] = it.user.description.toString(); content[9] = it.user.url.toString(); content[10] = it.user.followers_count.toString(); content[11] = it.user.friends_count.toString(); content[12] = it.user.listed_count.toString(); content[13] = it.retweet_count.toString(); content[14] = it.favorite_count.toString(); writer.writeNext(content) } writer.close(); endsubmit; quit; /* Import the final tweets data set */ proc import out=work.tweets datafile="&CSV_TWEET_FILE." dbms=csv replace; getnames=yes; datarow=2; run;quit;