Page 1 of 1

Tabulation with Alpha fields

Posted: September 16th, 2011, 5:26 am
by Guest
Is there any possibility to tabulate fields which are Alpha?

Re: Tabulation with Alpha fields

Posted: September 17th, 2011, 10:14 am
by Guest
Is it possible to convert alpha to numeric for tabulation?

Re: Tabulation with Alpha fields

Posted: September 22nd, 2011, 1:44 pm
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.

Re: Tabulation with Alpha fields

Posted: October 11th, 2011, 9:29 am
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;

Re: Tabulation with Alpha fields

Posted: October 12th, 2011, 12:57 pm
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;

Re: Tabulation with Alpha fields

Posted: March 23rd, 2012, 8:35 pm
by lls
Many thanks!

Re: Tabulation with Alpha fields

Posted: October 2nd, 2014, 11:12 pm
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.

Re: Tabulation with Alpha fields

Posted: April 16th, 2015, 9:22 am
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: