Tabulation with Alpha fields

Discussions about tabulating data in the designer or in batch mode
Post Reply
Guest

Tabulation with Alpha fields

Post by Guest »

Is there any possibility to tabulate fields which are Alpha?
Guest

Re: Tabulation with Alpha fields

Post by Guest »

Is it possible to convert alpha to numeric for tabulation?
Gregory Martin
Posts: 1845
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Tabulation with Alpha fields

Post by Gregory Martin »

Unfortunately, only numeric values can be tabulated. In general in CSPro, if you want to convert an alpha string to a number, you can use the tonumber function.

It would take some more advanced programming, but you could use the working storage dictionary, and the labels in it, to indirectly create a table from alpha values. Your tab logic might look something like this:
numeric i;

FAKE_STRING =
notappl;

do i = 1 while i <= 3

    
if getlabel(FAKE_STRING,i) = STRING then
        FAKE_STRING = i;
        
break;
    
endif;

enddo;
Download the attached program to see how this works.
Attachments
alphaTable.zip
(4.05 KiB) Downloaded 953 times
Guest

Re: Tabulation with Alpha fields

Post by Guest »

Tabulating from alpha field could be useful, especially with multiple choice check boxes.

Someone suggested me to use the code below to convert alpha into numeric, but it fails to compile.

Could someone advice how this code could be used or how it could be improved?

Thank you


function alphachk ( alpha(26) alphavar, alphlen, alpha(10) tail );

{ Function to check entry of alpha string variables }
{ alphlen: length of first consecutive part for the field, e.g. A-G is 7 }
{ tail: the residual characters in the valid string, i.e., X, Y, W, or Z }
alphanew = alphalst[1:alphlen]; { string of acceptable values }
alphanew = concat( strip(alphanew), strip(tail) );
totlen = length(strip(alphanew));
aok = 0; { if aok = 0, string is bad, if aok = 1, string is good; assume bad to start, i.e., empty string }
alphsize = length(strip(alphavar));
if alphsize then { not empty string }
{ check for "?" as only character }
aok = (alphavar[1:1] = "?" & alphsize = 1);
if !aok then { if not a single "?", check string }
aok = 1; { now assume string is good until we know otherwise }
l = 1; { l is position in string of acceptable values }
a = 1; { a is position in input string }
{ loop while the string is still good and there are more letters }
while aok & a <= alphsize do
aok = pos(alphavar[a:1],alphanew[l:totlen-l+1]);
if aok then { letter is acceptable }
l = l + aok; { increment l to position after letter in string of acceptable values }
a = a + 1 { increment a to next letter in input string }
endif
enddo
endif
endif;
alphachk = (!aok); { alphachk = 0 - if string is good, = 1 - if string is bad }
end;
Gregory Martin
Posts: 1845
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Tabulation with Alpha fields

Post by Gregory Martin »

The reason that this code may not have compiled for you was that you needed to declare several variables at the top of your code.

What this function does it check whether a string includes only valid multiple-response characters. The second parameter specifies the number of choices taken from the beginning of the alphabet. The third parameter allows for additional characters to be specified.

The function makes sure that each multiple choice selection is entered only once, and that the selections are entered in alphabetical order. See attached for an application that shows this, or look at this code:
PROC GLOBAL

alpha (26) alphalst = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

alpha (26) alphanew;
numeric totlen,aok,alphsize,l,a;

function alphachk ( alpha(26) alphavar, alphlen, alpha(10) tail );

    
{ Function to check entry of alpha string variables }
    
{ alphlen: length of first consecutive part for the field, e.g. A-G is 7 }
    
{ tail:    the residual characters in the valid string, i.e., X, Y, W, or Z }
    alphanew = alphalst[
1:alphlen];         { string of acceptable values }
    alphanew =
concat( strip(alphanew), strip(tail) );
    totlen =
length(strip(alphanew));
    aok =
0;      { if aok = 0, string is bad, if aok = 1, string is good; assume bad to start, i.e., empty string }
    alphsize =
length(strip(alphavar));
    
if alphsize then   { not empty string }
        
{ check for "?" as only character }
        aok = (alphavar[
1:1] = "?" & alphsize = 1);
        
if !aok then     { if not a single "?", check string }
            aok =
1;       { now assume string is good until we know otherwise }
            l =
1;         { l is position in string of acceptable values }
            a =
1;         { a is position in input string }
            
{ loop while the string is still good and there are more letters }
            
while aok & a <= alphsize do
                aok =
pos(alphavar[a:1],alphanew[l:totlen-l+1]);
                
if aok then     { letter is acceptable }
                    l = l + aok;  
{ increment l to position after letter in string of acceptable values }
                    a = a +
1     { increment a to next letter in input string }
                
endif
            
enddo
        
endif
    
endif;
    alphachk = (!aok);    
{ alphachk = 0 - if string is good, = 1 - if string is bad }
end;

PROC STRAD

    
if alphachk(STRAD,4,"") then
        
errmsg("Invalid combination. Can only enter A-D.");
        
reenter;
    
endif;

PROC STRAM

    
if alphachk(STRAM,13,"") then
        
errmsg("Invalid combination. Can only enter A-M.");
        
reenter;
    
endif;

PROC STRAD_XY

    
if alphachk(STRAD_XY,4,"XY") then
        
errmsg("Invalid combination. Can only enter A-D, X-Y.");
        
reenter;
    
endif;
Attachments
multCheck.zip
(3.01 KiB) Downloaded 887 times
lls
Posts: 109
Joined: December 6th, 2011, 3:11 pm
Location: Geneva, Switzerland

Re: Tabulation with Alpha fields

Post by lls »

Many thanks!
Butbi

Re: Tabulation with Alpha fields

Post by Butbi »

Sorry, I'm a newbee in CSPro. Can somebody help me with tutorial how to tabulate the multiple choice using alpha field. Applying "BreastFeeding" sample I am able to input the data in checkboxes.

Code: Select all

PROC Q1_BIETNHOMHOATDONG
onfocus
 opt5(1)=0;
 opt5(2)=0;
 opt5(3)=0;
 opt5(4)=0;
 opt5(5)=0;
 
killfocus
i=1;
   WHILE i <= 5 DO
   		IF Q1_BIETNHOMHOATDONG[i:1] in "1" THEN opt5(1)=1;
   		ELSEIF Q1_BIETNHOMHOATDONG[i:1] in "2" THEN opt5(2)=2;
		ELSEIF Q1_BIETNHOMHOATDONG[i:1] in "3" THEN opt5(3)=3;
		ELSEIF Q1_BIETNHOMHOATDONG[i:1] in "4" THEN opt5(4)=4;
		ELSEIF Q1_BIETNHOMHOATDONG[i:1] in "5" THEN opt5(5)=5;
		endif;
	    i = i + 1;
	ENDDO;
    Q1_1=opt5(1);
    Q1_2=opt5(2);
    Q1_3=opt5(3);
    Q1_4=opt5(4);
    Q1_5=opt5(5);
How can I analyze the values I got i.e frequency of Q1_1 ... Q1_5 from all cases.
I also tried with Fake_String sample but in my case I let the data input with a,b,c,d,e and not with numeric value so I cannot tabulate using this method.

Thank you for any support.
group
Posts: 1
Joined: April 16th, 2015, 9:22 am

Re: Tabulation with Alpha fields

Post by group »

if you want to convert an alpha string to a number, you can use the tonumber function. It would take some more advanced programming, but you could use the working storage dictionary, and the labels in it, to indirectly create a table from alpha values. Your tab logic might look something like this:
GuL
Post Reply