Simplifying Batch Exports


When CSPro has an interface for a tool, I suggest that you use the interface as much as possible, but on occasion a user might want to write code for more advanced functionality. One such example is when writing code for advanced export operations.

The CSPro help documents include some information on the export statement, but I often forget the syntax or what exactly the parameters should be. Fortunately, now with CSPro 4.1.002, a feature of the Export Data tool allows you to view the code that powers the export. By selecting Options -> Copy Logic to Clipboard, you can then take the export logic and insert it in the batch application of your choosing. This is a nice way to quickly get the basic export code needed and then you can build off of that.

For an example of a batch export that uses a lookup file, imagine that I have a data file describing stores and their customers. One of the attributes of a customer record is a country code that describes where the customer lives. When exporting the names of the customers, I do not want the two-letter country code, but the full name of the country. Using this list of country codes as a lookup file, I would go into the export tool, select the fields that I want to export, and then after copying the logic to the clipboard I would see code like this:

PROC GLOBAL
SET EXPLICIT;

NUMERIC rec_occ;

FILE cspro_export_file_var_f;

PROC COUNTRYCODEEXPORT_QUEST
PreProc
   
set behavior() export ( CommaDelim , ItemOnly );

   
For rec_occ in RECORD CUSTOMER_REC do
       
EXPORT TO cspro_export_file_var_f
       
CASE_ID(STORE_ID)
        STORE_REC, CUSTOMER_REC;
   
Enddo;

I would insert this code into a new batch application, add my lookup file code, and I would end up with something like this:

PROC GLOBAL

NUMERIC rec_occ;

FILE cspro_export_file_var_f;


PROC COUNTRYCODEEXPORT_FF


PROC COUNTRYCODEEXPORT_QUEST

PreProc

   
set behavior() export ( CommaDelim , ItemOnly );

   
For rec_occ in RECORD CUSTOMER_REC do

       
// look up the country name
        if not loadcase(COUNTRYCODES_DICT,CUSTOMER_COUNTRY) then
           
errmsg("Could not find country code: %s",CUSTOMER_COUNTRY);
            FULL_NAME = 
"<invalid>";
       
endif;

       
EXPORT TO cspro_export_file_var_f
       
CASE_ID(STORE_ID)
        STORE_REC, CUSTOMER_REC FULL_NAME; 
// FULL_NAME is added here (it comes from the lookup file)
    Enddo;

This feature in the Export Data tool vastly simplified my task, allowing me to focus on the lookup file programming, rather than the syntax of the export statement. My exported file now contains data from two files:

500  Pastry Pantry     Barack Obama          US  United States
500  Pastry Pantry     Angela Merkel         DE  Germany
500  Pastry Pantry     Hu Jintao             CN  China
800  Chocolate Heaven  Jacob Zuma            ZA  South Africa
800  Chocolate Heaven  Alexander Lukashenko  BY  Belarus

This is the input file:

10500Pastry Pantry
20500Barack Obama                                      US
20500Angela Merkel                                     DE
20500Hu Jintao                                         CN
10800Chocolate Heaven
20800Jacob Zuma                                        ZA
20800Alexander Lukashenko                              BY

Download this example.