Published on :
ETL SASHELP

Data Processing and XML Export

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins with a `%defaults` macro that initializes the global variable `dset` to `sashelp.class` if it is not already defined. Then, a `DATA _NULL_` is used to open the dataset specified by `&dset` and retrieve the number of observations (`nobs`), which is then stored in a global macro variable `nobs`. A dedicated XML section is then executed. It uses the `pathname(work)` function to get the path to the working directory, then defines an XML library named `test` that points to a `temp.xml` file in this directory. The content of the `&dset` dataset is then copied into this XML library, thus generating the `temp.xml` file. After freeing the libname `test`, the script reads the `temp.xml` file and displays its content via `_webout`. A part of the code for JSON generation is present but commented out. The script ends with `%STPBEGIN` and `%STPEND` declarations, indicating that it is designed to be executed as a SAS© stored process.
Data Analysis

Type : SASHELP


The script uses the `sashelp.class` dataset by default. This dataset is internal to SAS and always available.

1 Code Block
MACRO
Explanation :
This block defines and calls a `%defaults` macro. This macro checks for the existence of the macro variable `dset`. If `dset` does not exist, it declares it global and initializes it with the path `sashelp.class`.
Copied!
1%macro defaults ;
2 %* these defaults may be passed in, or ELSE they default ;
3 %IF %symexist(dset)=0 %THEN %DO;
4 %global dset;
5 %let dset=sashelp.class;
6 %END;
7%mend defaults;
8%defaults
2 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` step-by-step opens the dataset specified by `&dset` (by default `sashelp.class`), retrieves the number of observations (`nobs`) via the `attrn` function, and stores it in a macro variable named `nobs` using `call symput`.
Copied!
1DATA _null_ ;
2 dsid=open("&dset");
3 nobs=attrn(dsid,'nobs');
4 call symput('nobs',strip(put(nobs,8.)));
5RUN;
3 Code Block
DATA STEP Data
Explanation :
This block generates an XML file. It first retrieves the path to the working directory (`work`) and stores it in the `pathname` macro variable. An `xml` type libname `test` is then assigned to a `temp.xml` file in this directory. The content of the `&dset` dataset is copied into `test.test`, thus creating the XML file. The libname is then freed. Finally, a `DATA _NULL_` reads the `temp.xml` file and displays its content directly via `_webout`.
Copied!
1%let pathname=%sysfunc(pathname(work)) ;
2LIBNAME test xml "&pathname/temp.xml" ;
3DATA test.test ;
4 SET &dset ;
5RUN ;
6LIBNAME test ;
7DATA _null_ ;
8 INFILE "&pathname/temp.xml" ;
9 file _webout ;
10 INPUT ;
11 put _infile_ ;
12RUN ;
4 Code Block
MACRO
Explanation :
These lines define the delimiters for a SAS stored process. `%STPBEGIN` and `%STPEND` enclose the code that will be executed as part of the stored process. The line `*';*\"*/;run;` is standard syntax for potentially terminating pending code blocks or statements, often used to ensure clean execution within the context of stored processes.
Copied!
1%let _result=streamfragment ;
2%STPBEGIN;
3*';*";*/;run;
4%STPEND;
5*';*";*/;RUN;
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.