Multi select Options at their actual positions

Other discussions about CSPro
Forum rules
New release: CSPro 8.0
Post Reply
zeeshan
Posts: 4
Joined: November 21st, 2018, 7:56 am

Multi select Options at their actual positions

Post by zeeshan »

Dear All
Hope you are good. I am new here.

The problem I have been through is I want to write a function that will check all the option's values of the multiple select question from the valuesets and put them in an array. After doing so, that function will check the options entered by the interviewer.

The function will be able to place the selected options in their actual positions.

Right now, I am using the following logic to handle the problem:
PROC GLOBAL
array alpha (2) responses_2_1_3(6) = "01", "02", "03", "04","96","98";

PROC Q2_1_3
numeric ctr;
do ctr = 6 while ctr >= 1 by -1

if pos(responses_2_1_3(ctr),$) then
$[(ctr * 2 - 1):2] = responses_2_1_3(ctr);
else
$[(ctr * 2 - 1):2] = "";
endif;
enddo;

This logic has a problem that if INTERVIEWER selects the last option first and the 1st value at the end then this logic fails. Please help
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Multi select Options at their actual positions

Post by josh »

The problem is that you are modifying the field at the same time you are reading the values from it. Try setting the values into a temporary variable and then copying from the temporary into the field after all the values have been read.
numeric ctr;

string temp;

do ctr = 1 while ctr <= length(responses_2_1_3)

    
if pos(responses_2_1_3(ctr),$) then
        temp[(ctr *
2 - 1):2] = responses_2_1_3(ctr);
    
else
        temp[(ctr *
2 - 1):2] = "";
    
endif;
enddo;
zeeshan
Posts: 4
Joined: November 21st, 2018, 7:56 am

Re: Multi select Options at their actual positions

Post by zeeshan »

Oh thank you so much for the help. I don't know why shouldn't I think like that..

Another thing is: can't we write a general function in PROC GLOBAL that we just call in the PROC and will work for every multiple select question having different length and different options?

Like: Q2 have 3 options ("01", "02", "03")and Q4 have 6 options ("01","02","03","04","05","96")
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Multi select Options at their actual positions

Post by josh »

Yes you can write functions in proc global and use them in procs. http://www.csprousers.org/help/CSPro/us ... tions.html

Your function would be something like:
PROC GLOBAL

function string adjustCheckboxPositions(string fieldValue, array alpha(2) responses)

    
string temp;
    
    
do numeric ctr = 1 while ctr <= length(responses)
    
        
if pos(responses(ctr),fieldValue) then
            temp[(ctr *
2 - 1):2] = responses(ctr);
        
else
            temp[(ctr *
2 - 1):2] = "";
        
endif;
    
enddo;
    
    adjustCheckboxPositions = temp;
end;
And then to call it in the proc you could use:
PROC MTST
array alpha (2) responses_2_1_3(6) = "01", "02", "03", "04","96","98";
$ = adjustCheckboxPositions($, responses_2_1_3);
zeeshan
Posts: 4
Joined: November 21st, 2018, 7:56 am

Re: Multi select Options at their actual positions

Post by zeeshan »

Thanks for the help #Josh, I owe you one :D :D
CSProuserKS
Posts: 1
Joined: November 26th, 2018, 4:25 pm

Re: Multi select Options at their actual positions

Post by CSProuserKS »

Dear all,

Since 2012 I am using CSPro 4.1 for data collection.
Program was constructed in this program and that time we had 32 BIT in our laptops and PCs.
Now we bought new laptops and PCs but program does not support new devices due they are in 64 BIT.
I am asking is it any possibility to update program which I have in CSPro 4.1 in CSPro 7.1.3 or earlier version but to support our new devices or should I start from beginning to develop new program for data collection?

Thank you in advanced :)

Best regards and hope for your soon answers.
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Multi select Options at their actual positions

Post by josh »

CSPro is backwards compatible so you should be able to just open your 4.1 app in 7.1.

Next time please start a new topic rather than adding to an unrelated one.
zeeshan
Posts: 4
Joined: November 21st, 2018, 7:56 am

Re: Multi select Options at their actual positions

Post by zeeshan »

Dear Josh

The following errors are shown when I used your code to place "multiple select" options in their actual positions.

Code: Select all

ERROR:  Expecting left parenthesis '(' in a function call near line 9 in GLOBAL procedure
ERROR:  PostProc already defined near line 2 in Q2_1_3 procedure
The code I just copied :

Code: Select all

PROC GLOBAL

function string adjustCheckboxPositions(string fieldValue, array alpha(2) responses)

    string temp;
    
    do numeric ctr = 1 while ctr <= length(responses) 
    
        if pos(responses(ctr),fieldValue) then
            temp[(ctr * 2 - 1):2] = responses(ctr);
        else
            temp[(ctr * 2 - 1):2] = "";
        endif;
    enddo;
    
    adjustCheckboxPositions = temp;
end;

PROC Q2_1_3

array alpha (2) responses_2_1_3(6) = "01", "02", "03", "04","96","98";
$ = adjustCheckboxPositions($, responses_2_1_3);
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Multi select Options at their actual positions

Post by josh »

My apologies, it seems that passing an array to a function in this way does not work correctly in CSPro version 7.1. I have been using a beta version of 7.2 in which it does. The logic I posted for using a function will work in version 7.2 but not in version 7.1. For version 7.1 you will have to have separate logic for each multiple response field.
Post Reply