08 Batch Edit
08 Batch Edit
08 Batch Edit
To create a batch edit application you choose FileNew from CSPro and choose Batch Edit Application.
You then choose a dictionary. This is usually the same dictionary that you used for data entry.
The user interface for working with batch applications is similar to the one for working with data entry
applications except that there are no forms. Instead there is a tab for edits. Just like in data entry you
add logic to PROCS. Instead of running interactively, all the error messages are written out to a log file
for review after the whole program has run.
Next we run the application and choose the LesothoCensus2016.dat data file. After the application runs
we see the log file which reports that we have a case where this error exists.
Process Messages
1
User unnumbered messages:
To figure out what the problem is we can open up the problem case in data entry. The printout in the
listing file contains the case identifiers which we can use to find the case. You can copy the case id from
the listing file and use it with Find Case on the Edit menu in CSEntry.
Correcting Errors
In addition to using batch edit to find errors you can also use it to correct problems by modifying
variables in your logic. Let’s simply cap the age at first marriage to never be greater than the age.
When we run this time we will specify an output file: LesothoCensusEdited.dat. The changes we make
will only be made to the output file. We can then rerun the batch application on the output file and
make sure that you don’t have any error messages.
Instead of just assigning the value ofB20 we can use the impute command which does the assignment
just like “=” but also generates a nice report showing the values that were imputed.
The imputation report will be opened in TextViewer after you run the batch application but to see it you
will need to go to the Window menu and choose the file that ends in “.frq.lst”.
________________________________________________________________________________
Imputed Item B20: Age at first marriage - all occurrences
_____________________________ _____________
Categories Frequency CumFreq % Cum %
_______________________________ _____________________________ _____________
2
40 1 1 100.0 100.0
_______________________________ _____________________________ _____________
TOTAL 1 1 100.0 100.0
PROC ORPHAN
Run the program and look at the imputation report to see how many orphans are in our data set.
[Parameters]
DISTRICT=1
CONSTITUENCY=1
COMMUNITY_COUNCIL=B01
ZONE=2
SETTLEMENT=2
ENUMERATION_AREA=1010122018
The parameters that are passed via the pff are available in the application logic by using the function
sysparm(). Note that the fact that the parameter names are the same as the names of the id items does
not automatically set the id-items. We still need to set them in logic as follows:
PROC DISTRICT
preproc
3
// Fill in id-items if they were passed in as pff file parameters
if sysparm("DISTRICT") <> "" then
DISTRICT = tonumber(sysparm("DISTRICT"));
CONSTITUENCY = tonumber(sysparm("CONSTITUENCY"));
COMMUNITY_COUNCIL = sysparm("COMMUNITY_COUNCIL");
ZONE = tonumber(sysparm("ZONE"));
SETTLEMENT = tonumber(sysparm("SETTLEMENT"));
ENUMERATION_AREA = tonumber(sysparm("ENUMERATION_AREA"));
The sysparm() function always returns an alpha so for numeric values we use tonumber() to convert
them before assigning to numeric dictionary variables. Once we have filled in the values we use set
attributes protect to make the fields protected so that they cannot be modified. We could set these
fields as protected on the form but that would make it impossible to test our application without a pff
file that fills in the id items.
This works well but if we need to create a .pff file for each of 5000+ EAs it will take quite some time. We
have a list of all the EAs so we should be able generate the pff files automatically. In order to do this we
first need to see how to write out a pff file from CSPro logic.
PROC GLOBAL
file pffFile;
A file variable is a new type of variable that represents a file on the disk. We use the commands setfile(),
filewrite() and fileclose() to open, write to and close the file. The easiest way to figure out the logic for
writing out a pff file is to enable the advanced mode in the pff editor and to copy the logic from the
“View CSPro PFF File Creation Logic”. This will generate code to create and run the .pff. We can extract
just the part that writes out the pff file.
4
First we need to create a CSPro dictionary to match our EA spreadsheet. Let’s call our dictionary
“LesothoEAs”. We can simply copy and paste the id items from census dictionary that match the
spreadsheet columns into our new dictionary. We don’t need any regular dictionary variables in this
case; just the district, constituency, community council, zone, settlement and enumeration area.
Next we run the CSPro to Excel tool and pick the EA spreadsheet, the dictionary we just created and the
name for the CSPro data file and click convert. In other cases we might need to adjust the worksheet
number, start row or columns but in this case the defaults are fine. This generates a CSPro data file with
the same contents as the Excel spreadsheet.
PROC ENUMERATION_AREA
// Put generated pff files in folder called pffFiles since there will be many
string pffFileName = maketext("pffFiles/CensusEA%011d.pff",
ENUMERATION_AREA);
setfile(pffFile,pffFilename,create);
filewrite(pffFile,"[Run Information]");
filewrite(pffFile,"Version=CSPro 6.1");
filewrite(pffFile,"AppType=Entry");
filewrite(pffFile,"[Parameters]");
// This passes the ids as parameters to the census
// application which can access them via the sysparm() function.
filewrite(pffFile,"DISTRICT=%d", DISTRICT);
filewrite(pffFile,"CONSTITUENCY=%d", CONSTITUENCY);
filewrite(pffFile,"COMMUNITY_COUNCIL=%s", COMMUNITY_COUNCIL);
filewrite(pffFile,"ZONE=%d", ZONE);
filewrite(pffFile,"SETTLEMENT=%d", SETTLEMENT);
filewrite(pffFile,"ENUMERATION_AREA=%d", ENUMERATION_AREA);
filewrite(pffFile,"[DataEntryInit]");
filewrite(pffFile,"[Files]");
filewrite(pffFile,"Application=%s","./LesothoCensus2016.ent");
// Modify data file to be based on EA. This will avoid conflicts when
// syncing.
string dataFileName = maketext("CensusEA%011d.dat", ENUMERATION_AREA);
filewrite(pffFile,"InputData=%s",dataFileName);
close(pffFile);
5
We use maketext() to set the name of the pff and the name of the data file to include the EA code
otherwise the rest of the logic is copied from the pff editor.
When we run our application we should have a pff file for each EA listed in the Excel spreadsheet. These
can now be copied to the tablet along with the .pen file.
Exercises
1. Modify the batch edit application to convert the assets in H77 from alpha (used for checkboxes)
to a set of numeric yes/no variables. Create the new variables in the dictionary and write logic to
set the value of each of the new variables based on the letters in H77. The pos() function makes
this easy.
2. Modify the batch edit application to add a check for someone with relationship of spouse but
marital status that is not married. Print a message for each case found. This should be done in
the batch edit application NOT in the data entry application.
3. Modify the batch edit application to add a check that the total number of housing units in H68 is
equal to the sum of the numbers of each type of housing unit (rontabole, heisi…). This should be
done in the batch edit application NOT in the data entry application.
4. Modify the batch application that generates the .pff files to add a description to the generated
pff files. The description is what will appear in the list of applications when the file is copied to
an Android device. If no description is present then the name of the pff file used, which with the
generated pff files is not very user friendly. Make the description something more friendly.