The processed data (file date, time, size) comes from the textual output of a system command ('dir c:\windows\notepad.exe') executed via 'filename pipe'. The format of this output depends on the operating system and configured language, making this method potentially non-portable and prone to parsing errors if the 'dir' command's output format changes. The 'input' statement in the provided code is malformed, using JSON file paths as elements of the statement, which will prevent correct reading of data into the 'date', 'time', and 'size' variables.
1 Code Block
MACRO DÉFINITION
Explanation : This block defines the '%getstats' macro, which expects a 'dir_cmd' argument (a 'dir'-type system command). It uses 'filename pipe' to execute the specified system command and redirect its output to SAS, then opens this output for reading via 'infile cmd truncover'. A DATA step is used for internal processing without creating a dataset. The 'input #6' statement attempts to read the 6th line of the output. However, the presence of strings like ' @code_sas_json/...' is a syntax error in the 'input' statement and will prevent the correct reading of 'date', 'time', and 'size' values. Therefore, the variables will likely be missing. The 'call symput' functions are intended to store these values (which will likely be missing or incorrect) in the macro variables '_date', '_time', and '_size'.
Copied!
%macro getstats(dir_cmd) ;
filename cmd pipe "&dir_cmd" ;
data _null_ ;
infile cmd truncover ;
* This works on windows XP Professional, but on other version you might need to adjust the settings to
read the information you want from different positions ;
input #6 @code_sas_json/L3.13-Storing_a_List_of_Values_in_a_Macro-Variable.json date ddmmyy10.
@code_sas_json/L3.13-Storing_a_List_of_Values_in_a_Macro-Variable.json time time5.
@code_sas_json/q203.json size comma17. ;
* Now write the data to macro variables to be used ;
call symput('_date',put(date,date9.)) ;
call symput('_time',put(time,time5.)) ;
* note we use left justification within formatted field , otherwise number is right justified within field ;
call symput('_size',put(size,comma9. -l)) ;
run ;
%mend getstats ;
1
%macro getstats(dir_cmd) ;
2
filename cmd pipe "&dir_cmd" ;
3
DATA _null_ ;
4
INFILE cmd truncover ;
5
* This works on windows XP Professional, but on other version you might need to adjust the settings to
6
read the information you want from different positions ;
7
INPUT #6 @code_sas_json/L3.13-Storing_a_List_of_Values_in_a_Macro-Variable.json date ddmmyy10.
8
@code_sas_json/L3.13-Storing_a_List_of_Values_in_a_Macro-Variable.json time time5.
9
@code_sas_json/q203.json size comma17. ;
10
* Now write the data to macro variables to be used ;
11
call symput('_date',put(date,date9.)) ;
12
call symput('_time',put(time,time5.)) ;
13
* note we use left justification within formatted field , otherwise number is right justified within field ;
14
call symput('_size',put(size,comma9. -l)) ;
15
RUN ;
16
%mend getstats ;
2 Code Block
MACRO APPEL ET AFFICHAGE
Explanation : This block declares the macro variables '_date', '_time', and '_size' as global, so they can be used after the macro execution. It then calls the '%getstats' macro, passing the system command 'dir c:\windows\notepad.exe' as an argument, which triggers the macro's code execution. Finally, the '%put' statement attempts to display the values of the macro variables 'Date', 'Time', and 'Size' in the SAS log. Due to the parsing error in the 'input' statement within the macro, it is highly probable that these macro variables will not be correctly defined, and the '%put' will display empty values or errors.
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
« For enterprise-grade applications, avoid system pipes for file metadata. Instead, use the SAS internal functions DOPEN, FOPEN, and FINFO. These are OS-independent, do not require X-command privileges, and are far more reliable across different environments (Windows vs. Linux). »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.