Mutually exclusive check & contains code in alpha variables

Discussions about editing and cleaning data
Forum rules
New release: CSPro 8.0
Post Reply
Jamal Hossain
Posts: 5
Joined: August 24th, 2015, 6:39 am
Location: Dhaka, Bangladesh

Mutually exclusive check & contains code in alpha variables

Post by Jamal Hossain »

Hi, I am new user of CSPro, Currently, I am doing a nationwide Demographic Survey. I need help on some logical check.
I have two variables say, c13 is alpha numeric (150) and contains the 3 digit codes consecutive numeric values as.. 001002003045098124156235345 ...etc.;
and another variables c14 is also alpha numeric (15) and contains the 3 digit codes consecutive numeric values as 002045235 ..etc.;

The logic is --
1. codes of c14 should be in codes of C13.
2. c13 & c14 shold not blank (Blank check)
3. c13 & c14 should contain 3 digit codes (3 digit check)
4. range of code should be 1-400,999 (range check)

also, need another check for mutually exclusive check say for; C13 & C14 (mutually exclusive, c14 should not be in C13 )
Please help me by writing the programs on this regard !

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

Re: Mutually exclusive check & contains code in alpha variab

Post by josh »

Check 2 is easy, just compare C13 and C14 to the empty string "" to see if it is blank:

PROC C13
if C13 = "" then
errmsg("C13 cannot be blank");
reenter;
endif;

same for C14

For the other checks you need to be able to access the individual 3 digit codes within C13 and C14. To make this easier you can create a repeating subitem to let you access the individual 3 digit codes. Subitems are items in your dictionary that represent parts of a larger parent item. They don't use any extra storage. They share the storage with the larger item. You can think of them as a window into the larger parent item. For example to do this for C13 you would go into your dictionary and insert a new item just after C13, set the name to be C13_CODES, set the start position to the same start position as C13, set the item type to be subitem, set the type to be numeric, set the length to be 3 and set the number of occurrences to be 50. This will give you 50 items, each representing one of the 3 digit codes in the length 150 field C13.

In logic you can now refer to both the full 150 length field by using "C13" and each of the individual codes using a subscript on the repeating subitem e.g. C13_CODES(1) for the first 3 digit code, C13_CODES(2) for the second 3 digit code...

You can also add subitems to C14 the same way except that you would only have 5 occurrences instead of 50.

You can now loop through each of the subitems to check that they contain a 3 digit code in the range 1-400, 999 (check number 4 and also check number 3 since anything in the range 1-400, 999 is a 3 digit code).

// range check on C13
numeric i;
do i = 1 while i <= maxocc(C13)
if not C13_CODES(i) in 1-400,999 then
errmsg("Invalid code for C13");
reenter;
endif
enddo;

same thing for C14

Check 1 is a bit harder since you need to loop through each code in C14 and check that it is in C13 by looping through each of the codes in C13. For that I would write a function to check if a code is in C13:

function IsCodeInC13(code)
numeric i;
IsCodeInC13 = 0; // assume not found
do i = 1 while i <= maxocc(C13)
if code = C13_CODES(i) then
// found
IsCodeInC13 = 1;
break;
endif;
end;

Then you can use that function for each code in C14:

numeric i;
do i = 1 while i <= maxocc(C14)
if CodeInC13(C14_CODES(i)) = 0 then
errmsg("Code for C14 not found in C13");
reenter;
endif
enddo;

The C14 not in C13 check would be almost the same except that you would show the error message if CodeInC13 returns one instead of zero.
Jamal Hossain
Posts: 5
Joined: August 24th, 2015, 6:39 am
Location: Dhaka, Bangladesh

Re: Mutually exclusive check & contains code in alpha variab

Post by Jamal Hossain »

Hi Josh,
Thank you very much for the solutions. I works fine.
Actually, I am looking for the loop (if any) C13 and using memory variables for verifying the duplicate values. My idea is like below- but this works just for consecutive 2 values not so for others, range check is possible here. Can you please think about this type of loop. It would be great if it works....

numeric i;
i=1;
do i = 1 while i < 47
if (tonumber($[((3*i)-2):3])>0 and (tonumber($[((3*i)-2):3]) = tonumber($[((3*i)+1):3]))) then
errmsg("duplication in codes");
reenter;
endif;

if (tonumber($[((3*i)-2):3])>400 OR tonumber($[((3*i)+1):3])>400) then
errmsg("range check 1-400");
reenter;
endif;
enddo;


regards,
Jamal
Jamal Hossain
Posts: 5
Joined: August 24th, 2015, 6:39 am
Location: Dhaka, Bangladesh

Re: Mutually exclusive check & contains code in alpha variab

Post by Jamal Hossain »

Hi Josh,

Greetings ! At last I have created a loop for variable c13 to check duplicate codes

PROC c13
Numeric i,j;
i=1;
j=i+1;

//Checking duplication in Points
do i = 1 while i <=47
do j = i+1 while j <=47
IF(tonumber(c13[((3*j)-2):3])>0 and (tonumber(c13[((3*i)-2):3]) = tonumber(c13[((3*j)-2):3]))) then
errmsg ("Duplication in codes- RECHECK CODES ");
Endif;
enddo;
enddo;
skip to c14;

It works fine. Can you help me out - To see in errmsg- Which codes are duplicate ?

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

Re: Mutually exclusive check & contains code in alpha variab

Post by josh »

To add the duplicate codes just use %d in the errmsg (see help on errmsg for details).

You should really use sub-items instead of tonumber(c13[((3*i)-2):3]). It will really simplify your code.
Post Reply