Because this repository
contains only one task, the code in the tasks.xml file is pretty simple.
<?xml version="1.0" encoding="UTF-8"?>
<Tasks>
<Task>
<Name label="Generate QR Code"/>
<Template uri="./qr_code.ctm"/>
<Icon uri="https://cdn2.iconfinder.com/data/icons/
windows-8-metro-style/512/qr_code.png"/>
</Task>
</Tasks>
The
Template
element
refers to the CTM file that contains the XML code, Apache Velocity
code, and SAS code needed to run the task. (For more information about
how to write a SAS Studio task, see
SAS Studio: Developer's Guide to Writing Custom Tasks.)
Because this CTM file is saved in the repository, the
Template
element
uses a relative path.
Here is the code in
the qr_code.ctm file:
<?xml version="1.0" encoding="UTF-8"?>
<Task schemaVersion="4.0" runNLS="never">
<Registration>
<Name>Generate a QR Code</Name>
<Description>Generate a QR Code</Description>
<GUID>*</GUID>
<Procedures>HTTP</Procedures>
<Version>3.5</Version>
<Links>
<Link href="http://support.sas.com/kb/45/594.html">Generating a QR Code
using PROC HTTP</Link>
</Links>
</Registration>
<Metadata>
<DataSources>
</DataSources>
<Options>
<Option name="OPTIONSTAB" inputType="string">OPTIONS</Option>
<Option name="textEXAMPLE" defaultValue="My QR Code"
inputType="inputtext"
indent="1"
required="true"
promptMessage="Text for your QR Code"
missingMessage="Missing text.">Input text:</Option>
</Options>
</Metadata>
<UI>
<Container option="OPTIONSTAB">
<OptionItem option="textEXAMPLE"/>
</Container>
</UI>
<CodeTemplate>
<![CDATA[
filename url url "{baseURL}/qr_macros.sas";
%include url;
filename code temp;
data _null_;
file code;
put "$textEXAMPLE";
output;
run;
%generate_qr_code(code,_dataout);
%let _DATAOUT_MIME_TYPE=image/png;
%let _DATAOUT_NAME=qrcode.png;
]]>
</CodeTemplate>
</Task>
In the CTM file, the Registration
, Metadata
,
and UI
elements define the user interface
for the task.
The content of the CodeTemplate
element
generates the SAS code that is needed to run the task. The FILENAME
statement uses the URL Access Method to point to the qr_macros.sas
file, which contains the macro code.
Here is the code for
the qr_macro.sas file:
/* Have whatever text you want in the first fileref,
and reference a PNG file in the second fileref.
Use this macro to produce a QR image of the text.
You can have up to 4K of text in the input file.
For example:
filename mypgm 'mypgm.sas';
filename myqr 'qr.png';
%to_qr ( mypgm , myqr );
*/
%macro generate_qr_code(in,out);
/*this data step generates a string of attributes required by the web service.*/
data; infile &in recfm=f lrecl=4096 length=l;
length url_encoded $8192;
keep url_encoded;
input @1 all $varying4096. l;
url_encoded = 'chs=500x500&cht=qr&chl=' ||
urlencode(strip(all)) || '&chld=l';
call symputx('qrtextl',length(url_encoded));
output; stop;
run;
filename qrtext temp recfm=f lrecl=&qrtextl;
/* This data step writes to a text file the input that PROC HTTP
will use to call the web service. */
data _null_;
set;
file qrtext;
put url_encoded;
run;
proc delete; run;
/*call the web service*/
proc http in=qrtext out=&out method='post'
url='https://chart.googleapis.com/chart?'
ct='application/x-www-form-urlencoded';
run;
filename qrtext clear;
%mend;