Dynamic Value Sets With the New ValueSet Object


CSPro 7.3 introduces new ways to work with dynamic value sets. Dynamic value sets define the acceptable options for a field and they vary based on responses previously given. Typical value sets, defined in the data dictionary, define a fixed set of responses for a field, but with a dynamic value set, you can customize these responses based on specific conditions.

Prior to CSPro 7.3, you could create dynamic value sets using arrays, but working with these was cumbersome and not intuitive. Now there is a valueset object that allow for simpler, and more sophisticated, value set creation. Four scenarios are presented below that show how to use the new valueset object.

Easily Create a Dynamic Value Set in a Loop

A typical task is to create a value set based on some attributes entered previously. For example, you might want to present a list of people in a household who are aged 15+ as eligible heads of household. Using the valueset object with a for loop with a where condition makes this task trivial:

PROC HOUSEHOLD_HEAD

preproc

    valueset
household_head_vs;

   
for numeric line_number in PERSON_ROSTER where AGE >= 15 do
       
household_head_vs.add(NAME, line_number);
   
endfor;

   
setvalueset(HOUSEHOLD_HEAD, household_head_vs);

Combining Value Sets

Suppose you have a question that asks about the way that someone deceased. In the dictionary there is one set of responses that applies to all people and an additional set of responses that applies to females aged 12+. Now you can easily create a dynamic value set, conditionally adding the female aged 12+ responses:

PROC MORTALITY_REASON

onfocus

    valueset
mortality_reason_vs = MORTALITY_REASON_ALL_PEOPLE_VS;

   
if SEX = 2 and AGE >= 12 then
       
mortality_reason_vs.add(MORTALITY_REASON_FERTILE_WOMEN_VS);
   
endif;

   
setvalueset(MORTALITY_REASON, mortality_reason_vs);

Removing a Value Based on a Previous Selection

Sometimes a questionnaire has a series of questions that asks about preferences, such as, “What is your favorite color?,” and then, “What is your second favorite color?” The list of options for the second question can exclude the selected answer to the first question. The valueset object makes this task very easy:

PROC SECOND_FAVORITE_COLOR

preproc

    valueset
second_favorite_color_vs = FAVORITE_COLOR_VS;

    second_favorite_color_vs.remove(FAVORITE_COLOR);

   
setvalueset(SECOND_FAVORITE_COLOR, second_favorite_color_vs);

Iterate Through Value Set Codes and Labels

Finally, there are two lists that are part of a value set, accessed using the codes and labels attributes. Just as valueset is a new object in CSPro 7.3, lists, though around in some form for years, are now fully useable objects. This simplifies iterating through the codes and labels of a value set. For example, if the first two digits of the county code are equal to the state code, a dynamic value set for counties could be created as follows:

PROC COUNTY

preproc

    valueset
filtered_county_vs;

   
numeric first_county_code = STATE * 100;
   
numeric last_county_code = first_county_code + 99;

   
do numeric counter = 1 while counter <= COUNTY_VS.codes.length()

       
if COUNTY_VS.codes(counter) in first_county_code:last_county_code then
           
filtered_county_vs.add(COUNTY_VS.labels(counter), COUNTY_VS.codes(counter));
       
endif;

   
enddo;

   
setvalueset(COUNTY, filtered_county_vs);

#Logic