Published on :
Général COMMANDE_SYSTEME_EXTERNE

Sans titre

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
The '%getstats' macro takes one argument, 'dir_cmd', which represents the system command to execute (e.g., 'dir c:\path\to\file.exe'). It uses 'filename pipe' to capture the output of this command. A DATA step attempts to read the 6th line of this output to extract the file's date, time, and size. These values are then stored in the global macro variables '_date', '_time', and '_size' via 'call symput'. It is important to note that the 'input' statement contains invalid JSON paths as elements, which will cause data extraction to fail and result in an error. After the macro call, the global variables are displayed in the SAS© log. The 'dir' command is specific to Windows, and its output format may vary, requiring adjustments for other operating systems or versions, and the robustness of the parsing heavily depends on the consistency of the system command's output format.
Data Analysis

Type : COMMANDE_SYSTEME_EXTERNE


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!
1%macro getstats(dir_cmd) ;
2filename cmd pipe "&dir_cmd" ;
3DATA _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)) ;
15RUN ;
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.
Copied!
1%global _date _time _size ;
2%getstats(dir c:\windows
3otepad.exe) ;
4%put Date=&_date Time=&_time Size=&_size ;
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.
Banner
Expert Advice
Expert
Stéphanie
Spécialiste Machine Learning et IA.
« 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). »