forked from Jiangtang/SAS_ListProcessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqreadpipe.sas
67 lines (64 loc) · 2.67 KB
/
qreadpipe.sas
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
/*<pre><b>
/ Program : qreadpipe.sas
/ Version : 2.1
/ Author : Roland Rashleigh-Berry (http://www.datasavantconsulting.com/roland/)
/ Date : 23-Sep-2011
/ Purpose : Function-style macro to read the output of a system command and
/ return the result trimmed and MACRO QUOTED.
/ SubMacros : %qtrim
/ Notes : Result will be MACRO QUOTED. Use %unquote to make the string
/ output usable in ordinary sas code.
/ Usage : %let mvar=%qreadpipe(echo $USER);
/ %put %qreadpipe(echo '%username%');
/===============================================================================
/ PARAMETERS:
/-------name------- -------------------------description------------------------
/ command (pos) System command. This should not be enclosed in quotes
/ but may be enclosed in %str(), %quote() etc..
/===============================================================================
/ AMENDMENT HISTORY:
/ init --date-- mod-id ----------------------description------------------------
/ rrb 13Feb07 "macro called" message added
/ rrb 22Jul07 Header tidy
/ rrb 30Jul07 Header tidy
/ rrb 31Oct08 Major redesign for v2.0
/ rrb 12Oct09 Macro renamed from readpipe to qreadpipe (v2.1)
/ rrb 04May11 Code tidy
/ rrb 23Sep11 Header tidy
/===============================================================================
/ This is public domain software. No guarantee as to suitability or accuracy is
/ given or implied. User uses this code entirely at their own risk.
/=============================================================================*/
%macro qreadpipe(command);
%local fname fid str rc res err;
%let err=ERR%str(OR);
%let rc=%sysfunc(filename(fname,&command,pipe));
%if &rc NE 0 %then %do;
%put &err: (qreadpipe) Pipe file could not be assigned due to the following:;
%put %sysfunc(sysmsg());
%end;
%else %do;
%let fid=%sysfunc(fopen(&fname,s,80,b));
%if &fid EQ 0 %then %do;
%put &err: (qreadpipe) Pipe file could not be opened due to the following:;
%put %sysfunc(sysmsg());
%end;
%else %do;
%do %while(%sysfunc(fread(&fid)) EQ 0);
%let rc=%sysfunc(fget(&fid,str,80));
%let res=&res%superq(str);
%end;
%qtrim(&res)
%let rc=%sysfunc(fclose(&fid));
%if &rc NE 0 %then %do;
%put &err: (qreadpipe) Pipe file could not be closed due to the following:;
%put %sysfunc(sysmsg());
%end;
%let rc=%sysfunc(filename(fname));
%if &rc NE 0 %then %do;
%put &err: (qreadpipe) Pipe file could not be deassigned due to the following:;
%put %sysfunc(sysmsg());
%end;
%end;
%end;
%mend qreadpipe;