Restrict numbering

Other discussions about CSPro
Forum rules
New release: CSPro 8.0
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Restrict numbering

Post by MrTaco »

Hi Guys

I need a restrict rule for restricting a province according to district codes
eg. Province = 1 (District code = 101 - 108), so i want to affiliate 101 - 108 to province 1..
and so no capturing will done outside those bracket.

I tried this rule... but it's not working

if PROVINCE = 1 elseif then DISTRICT >= 101 and then DISTRICT <= 108
then skip to SCHOOL_QUESTIONNAIRE_NUMBER;
if PROVINCE = 2 elseif then DISTRICT >= 221 and then DISTRICT <= 228
then skip to SCHOOL_QUESTIONNAIRE_NUMBER;endif

Thabiso
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Restrict numbering

Post by josh »

Try something like the following:
if PROVINCE = 1 then
    
if not DISTRICT in 101:108 then
       
errmsg("Invalid district");
       
reenter;
    
endif;
elseif PROVINCE = 2  then
    
if not DISTRICT in 221:228 then
       
errmsg("Invalid district");
       
reenter;
    
endif;
endif;
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Restrict numbering

Post by MrTaco »

Thanks Josh it worked...
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Restrict numbering

Post by MrTaco »

i also want to restrict Barcode with Response code

Response code = 1 means there's blood taken and there's blood taken.
Response code = 2 means no blood taken and there's no barcode.
it shouldn't allow 2 if there's barcode on response code..
and it shouldn't allow 1 if there's no barcode on response code..
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Restrict numbering

Post by MrTaco »

Hi Josh

I have just realized that "0" doesn't appear when when you write any number that starts with "0"
what can I can do to make sure it appears.

Thanks
Thabiso
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Restrict numbering

Post by josh »

On Windows if you set zero-fill in the data dictionary to true for a numeric item then leading zeros will appear. On Android the leading zeros will not be displayed when editing the number but the data will be stored with the leading zeros in the data file.
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Restrict numbering

Post by MrTaco »

Hi Josh
I did set zero fill on my dict but now when i convert it to SPSS it doesn't show the "0"

it only show on the data capturing file

Thanks
Thabiso
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Restrict numbering

Post by josh »

There may be a setting in SPSS. I don't have SPSS so I don't know. Try looking in the SPSS documentation or maybe someone else on the forum who uses SPSS can help.
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Restrict numbering

Post by MrTaco »

if I use alphanumeric is the a code that I can apply to only accept numeric "0123456789"
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Restrict numbering

Post by josh »

You could convert the alpha to a number using the tonumber()function and if it is not a number then the result will be the special value "default". Something like:
if tonumber(MYALPHA) = default then
  
errmsg("Enter a valid number");
  
reenter;
endif;
The limitation here is that if you have something that starts with a number but has non-number characters at the end then it will not give you an error. For example tonumber("Hello1234") will give you default and fail the above test but tonumber("1234Hello") will result in the number 1234 and pass. This will also work with decimal numbers.

If you want to be more strict you will need to loop through each digit and make sure it is a number. You can create a function to do this:
// Determine if a string is made up of all numeric digits
function isNumeric(string s)
    
numeric i;
    isNumeric =
1; // assume is number
    do i = 1 while i <= length(strip(s))
        
// check if ith is a number between 0 and 9
        if pos(s[i:1], "0123456789") = 0 then
            
// it is not so set return value to false
            isNumeric = 0;
            
break;
        
endif;
    
enddo;
end;

Then you can use it like this:
if not isNumeric(MYALPHA) then
    
errmsg("Enter a valid number");
    
reenter;
endif;
Post Reply