The selcase (“select case”) function is used to display a list of cases in an external dictionary, letting an interviewer select a case to load. One function not mentioned in that page’s help documentation is the ability for the user to select multiple cases. By using the multiple keyword, the interviewer can select more than one case and then iterate over each of those cases using a for loop.
An undocumented feature allows for all qualified cases to be automatically marked. Using the automark keyword, the selcase dialog is not shown to the interviewer. These two sets of code are the same:
Because selcase allows you to pass a key to match (“” in the example above, which means all case keys), you can use this as a trick to efficiently create value sets if you know what part of the key is. For example, if you have a data file with 50,000 cases, forcase will always loop through all 50,000 cases, whereas providing a key match may limit your loop to substantially fewer cases.
To show a possible use for this trick, we will look at two ways of creating hierarchical value sets for geocodes. Supposing we have three levels of geography—Region, District, and EA—one way to structure a geocode lookup file is as follows:
|1||2||District 2 in Region 1|
|1||2||5||EA 5 in Region 1 / District 2|
That is, when defining regions, the district and EA codes are left blank, and when defining districts, the EA code is left blank.
Using a forcase loop to populate the districts based on a selected region, we would loop over the entire data file, filtering on cases where the geocode region matches the selected region, where the geocode district is defined, but where the geocode EA is blank:
Prior to this, to generate the region value set, we would look for cases where the geocode district is blank, and then following this, to generate the EA value set, we would look for cases where the region and district match the selected codes and where the EA is defined. To generate the hierarchical value sets for the three levels of geography would require fairly different loops.
With the selcase automark trick, we can create a single function that can be used to generate the value set for each level of geography:
We call this function from each procedure, specifying the currently selected geocode and the geocode length at each level. In this example, we assume that the region is length 1 and that the other two geocodes are length 2:
Now we have a generalizable function that we can use in our censuses or surveys, a function that will work with any number of levels of geography.