Skip to content

Macro SMILE_URL_DOWNLOAD

Downloads a file from an URL and store it locally on OUTFILE. Additionally return code can be stored and information can optionally be printed to the log.

  • Author: Katja Glass
  • Date: 2021-01-04
  • SAS Version: SAS 9.4
  • License: MIT
  • Comment: Return codes are 0 - URL found, 999 - no URL or OUTFILE provided, 998 - URL or OUTFILE not provided in quotes, otherwise html-return code (e.g. 404 file not found)
  • Reference: The idea from this macro is coming from a paper by Joseph Henry - The ABCs of PROC HTTP (https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2019/3232-2019.pdf)

Parameters

Parameter Description
URL http(s) URL which should be checked in quotes
OUTFILE output file provided in quotes
RETURN return code variable (scope should be global)
INFO NO/YES indicator to print information to the log


Examples

%smile_url_download(url="http://sas.cswenson.com/downloads/macros/AddFormatLib.sas",
                   outfile="/folders/myshortcuts/git/sas-dev/packages/chris_sas_macros/AddFormatLib.sas",
                   info=NO,
                   return=);

Checks

  • URL and OUTFILE must be provided;
  • URL and OUTFILE must be provided in quotes;

Macro

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%************************************************************************************************************************;
%* Project    : SMILE - SAS Macros, Intuitive Library Extension
%* Macro      : smile_url_download
%* Parameters : URL     - http(s) URL which should be checked in quotes
%*              OUTFILE - output file provided in quotes
%*              RETURN  - return code variable (scope should be global)
%*              INFO    - NO/YES indicator to print information to the log
%*
%* Purpose    : Downloads a file from an URL and store it locally on OUTFILE. Additionally return code can be stored and
%*              information can optionally be printed to the log.
%* Comment    : Return codes are 0 - URL found, 999 - no URL or OUTFILE provided,
%*              998 - URL or OUTFILE not provided in quotes, otherwise html-return code (e.g. 404 file not found)
%*
%* Author     : Katja Glass
%* Creation   : 2021-01-04
%* License    : MIT
%*
%* Reference  : The idea from this macro is coming from a paper by Joseph Henry - The ABCs of PROC HTTP
%*              (https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2019/3232-2019.pdf)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%smile_url_download(url="http://sas.cswenson.com/downloads/macros/AddFormatLib.sas",
                   outfile="/folders/myshortcuts/git/sas-dev/packages/chris_sas_macros/AddFormatLib.sas",
                   info=NO,
                   return=);
*/
%************************************************************************************************************************;

%MACRO smile_url_download(url=, outfile=, info=NO, return=);

   %LOCAL macro issue;
   %LET macro = &sysmacroname;
   %LET issue = 0;

   %* check: URL and OUTFILE must be provided;
   %IF %LENGTH(&url) = 0 OR %LENGTH(&outfile)
   %THEN %DO;
       %PUT %STR(ERR)OR: &macro - URL and OUTFILE must be provided.;
       %IF %LENGTH(&return) > 0
           %THEN %LET &return = 999;
       %RETURN;
   %END;

   %* check: URL and OUTFILE must be provided in quotes;
   DATA _NULL_;
       ATTRIB url FORMAT=$2000.;
       ATTRIB outfile FORMAT=$2000.;
       url = SYMGET('url');
       IF NOT (SUBSTR(url,1,1) IN ("'",'"') AND SUBSTR(url,LENGTH(url))  IN ("'",'"'))
       THEN DO;
           PUT "ERR" "OR: &macro - URL must be provided in quotes.";
           %IF %LENGTH(&return) > 0
               %THEN CALL SYMPUT("&return", "998");
           CALL SYMPUT("issue", "1");
       END;
       outfile = SYMGET('outfile');
       IF NOT (SUBSTR(outfile,1,1) IN ("'",'"') AND SUBSTR(outfile,LENGTH(outfile))  IN ("'",'"'))
       THEN DO;
           PUT "ERR" "OR: &macro - OUTFILE must be provided in quotes.";
           %IF %LENGTH(&return) > 0
               %THEN CALL SYMPUT("&return", "998");
           CALL SYMPUT("issue", "1");
       END;
   RUN;
   %IF &issue = 1
       %THEN %RETURN;

   FILENAME out &outfile;
   FILENAME hdrs TEMP;

   PROC HTTP URL=&url
       OUT=out
       HEADEROUT=hdrs;
   RUN;

   DATA _NULL_;
       INFILE hdrs SCANOVER TRUNCOVER;
       INPUT @'HTTP/1.1' code 4. message $255.;

       %IF %LENGTH(&return) > 0
       %THEN %DO;
           IF code=200
               THEN CALL SYMPUT("&return", "0");
               ELSE CALL SYMPUT("&return", code);
       %END;
       %IF %UPCASE(&info) NE NO
       %THEN %DO;
           PUT "Return Code: " code;
           PUT "Return Message: " message;
       %END;
   RUN;

%MEND smile_url_download;