Published on :

Library to CARDS File Conversion

This code is also available in: Deutsch Español Français
Awaiting validation
This macro iterates through all tables in a specified library (via the 'lib' parameter). For each table, it uses the %mp_ds2cards macro to convert the data into SAS© code (DATA step with cards). The result is written to a defined output directory. The user can choose to consolidate all code into a single file ('outfile') or create a separate '.sas©' file for each table. The macro also handles the creation of the destination directory if necessary.
Data Analysis

Type : MIXTE


The script queries the DICTIONARY.TABLES system view to list the library's contents. It then reads the data from the library passed as a parameter ('lib') for export.

1 Code Block
PROC SQL
Explanation :
This step retrieves the list of table names (members) present in the library specified by the &lib parameter by querying the SAS dictionary. The list is stored in the &memlist macro variable.
Copied!
1PROC SQL noprint;
2select distinct lowcase(memname)
3 into: memlist
4 separated BY ' '
5 from dictionary.tables
6 where upcase(LIBNAME)="%upcase(&lib)";
2 Code Block
Macro Call
Explanation :
Cleans the output directory path (removes trailing slashes) and ensures the directory exists by calling the %mf_mkdir utility macro.
Copied!
1%let outloc=%mf_trimstr(&outloc,/);
2%let outloc=%mf_trimstr(&outloc,\);
3 
4/* create the output directory */
5%mf_mkdir(&outloc)
3 Code Block
Macro Loop Data
Explanation :
Loops through each table found in the library. Calls the %mp_ds2cards macro to generate the corresponding CARDS file. Manages the logic of appending to a single file (append=YES) or creating separate files depending on the value of the &outfile parameter.
Copied!
1%DO x=1 %to %sysfunc(countw(&memlist));
2 %let ds=%scan(&memlist,&x);
3 %mp_ds2cards(base_ds=&lib..&ds
4 ,maxobs=&maxobs
5 ,random_sample=&random_sample
6 %IF "&outfile" ne "0" %THEN %DO;
7 ,append=YES
8 ,cards_file="&outloc/&outfile"
9 %END;
10 %ELSE %DO;
11 ,append=NO
12 ,cards_file="&outloc/&ds..sas"
13 %END;
14 )
15%END;
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.
Copyright Info : Allan Bowe