Decimals when Creating "Binary" Variable

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Decimals when Creating "Binary" Variable

Eric Black
I have a list of 7 dummy variables and I want to have one variable with all
combinations - in the binary format, i.e. 0010000  means that only the third
dummy is a 1 and all others are 0.
When I do this with the command below, I do get my results but some
combinations yield some odd decimal issue (i.e. case ID 3 is
1001.0000000000001 instead of 0001001)

*Begin***.
data list list / id item1  item2 item3 item4 item5 item6 item7 (8f2.0).
begin data
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 1
3 0 0 0 0 0 1 0
4 1 0 0 1 0 0 0
end data.


numeric combination (N7.0).
      compute combination= 1000000*item7 +
                                                100000*item6 +
                                               10000*item5 +
                                               1000*item4 +
                                               100*item3 +
                                               10*item2 +
                                               item1.

fre combination.
*End***.

This will give me the following:

combination
  Frequency Percent Valid Percent Cumulative Percent
Valid 0000000 1 25.0 25.0 25.0
 1001.0000000000001 1 25.0 25.0 50.0
 0100000 1 25.0 25.0 75.0
 1000000 1 25.0 25.0 100.0
 Total 4 100.0 100.0

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Stan Gorodenski
Matt,

On 2/6/2013 9:39 AM, Matt Black wrote:
> I have a list of 7 dummy variables and I want to have one variable with all
> combinations - in the binary format, i.e. 0010000  means that only the third
> dummy is a 1 and all others are 0.
> When I do this with the command below, I do get my results but some
> combinations yield some odd decimal issue (i.e. case ID 3 is
> 1001.0000000000001 instead of 0001001)
>

 From the copy of the printout you gave, it appears it is case 2 and not
3, and it should begin with a 1 because item7=1. If I understand your
algorithm correctly, it should be 1000000. The only thing I can think of
is that maybe there is something odd about the placement of the values
in the begin data-end data. For example, you have F2 for all variables,
but id should really be F1. This is just a wild guess. Maybe you could
try "Data List Free" and drop the (8f2.0) and then for each variable,
such as item7, in your algorithm use trunc(item7), for example, and the
same for all the other variables.
Stan

> *Begin***.
> data list list / id item1  item2 item3 item4 item5 item6 item7 (8f2.0).
> begin data
> 1 0 0 0 0 0 0 0
> 2 0 0 0 0 0 0 1
> 3 0 0 0 0 0 1 0
> 4 1 0 0 1 0 0 0
> end data.
>
>
> numeric combination (N7.0).
>        compute combination= 1000000*item7 +
>                                                  100000*item6 +
>                                                 10000*item5 +
>                                                 1000*item4 +
>                                                 100*item3 +
>                                                 10*item2 +
>                                                 item1.
>
> fre combination.
> *End***.
>
> This will give me the following:
>
> combination
>    Frequency Percent Valid Percent Cumulative Percent
> Valid 0000000 1 25.0 25.0 25.0
>   1001.0000000000001 1 25.0 25.0 50.0
>   0100000 1 25.0 25.0 75.0
>   1000000 1 25.0 25.0 100.0
>   Total 4 100.0 100.0
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Stan Gorodenski
On 2/6/2013 10:07 AM, Stan Gorodenski wrote:

> Matt,
>
> On 2/6/2013 9:39 AM, Matt Black wrote:
>> I have a list of 7 dummy variables and I want to have one variable
>> with all
>> combinations - in the binary format, i.e. 0010000  means that only
>> the third
>> dummy is a 1 and all others are 0.
>> When I do this with the command below, I do get my results but some
>> combinations yield some odd decimal issue (i.e. case ID 3 is
>> 1001.0000000000001 instead of 0001001)
>>
>
> From the copy of the printout you gave, it appears it is case 2 and not
> 3,

Opps. It just occurred to me that I am forgetting this is a freq and not
a list. You may be right that it is case 3. I won't bother trying to
verify. I should have known better than to help someone on line. When I
write code it frequently occurs to me later where I made an error, and
the same happens on a discussion group. Anyway, I would suggest doing
what I said below, or maybe all it takes is to enter a space in front of
each id value. Like I said, this is just a wild guess. I haven't
attempted to reproduce what you are doing, but it should work, as far as
I can see. It is fairly simple, unless, again, I am not seeing something
I would see if I tried to reproduce it.
Stan

> and it should begin with a 1 because item7=1. If I understand your
> algorithm correctly, it should be 1000000. The only thing I can think of
> is that maybe there is something odd about the placement of the values
> in the begin data-end data. For example, you have F2 for all variables,
> but id should really be F1. This is just a wild guess. Maybe you could
> try "Data List Free" and drop the (8f2.0) and then for each variable,
> such as item7, in your algorithm use trunc(item7), for example, and the
> same for all the other variables.
> Stan
>
>> *Begin***.
>> data list list / id item1  item2 item3 item4 item5 item6 item7 (8f2.0).
>> begin data
>> 1 0 0 0 0 0 0 0
>> 2 0 0 0 0 0 0 1
>> 3 0 0 0 0 0 1 0
>> 4 1 0 0 1 0 0 0
>> end data.
>>
>>
>> numeric combination (N7.0).
>>        compute combination= 1000000*item7 +
>>                                                  100000*item6 +
>>                                                 10000*item5 +
>>                                                 1000*item4 +
>>                                                 100*item3 +
>>                                                 10*item2 +
>>                                                 item1.
>>
>> fre combination.
>> *End***.
>>
>> This will give me the following:
>>
>> combination
>>    Frequency Percent Valid Percent Cumulative Percent
>> Valid 0000000 1 25.0 25.0 25.0
>>   1001.0000000000001 1 25.0 25.0 50.0
>>   0100000 1 25.0 25.0 75.0
>>   1000000 1 25.0 25.0 100.0
>>   Total 4 100.0 100.0
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>> [hidden email] (not to SPSSX-L), with no body text except the
>> command. To leave the list, send the command
>> SIGNOFF SPSSX-L
>> For a list of commands to manage subscriptions, send the command
>> INFO REFCARD
>>
>>
>>
>>
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Bruce Weaver
Administrator
In reply to this post by Eric Black
When I run your code (using version 20.0.0.1 under Windoze 7), I don't get that problem.  What version are you running?  (Another current thread is reporting strange behaviour of AGGREGATE's SUM function in v21.)  

You could use TRUNC to eliminate anything to the right of the decimal.

Meanwhile, when I looked at your COMPUTE, my first thought was that I would do it like this (for better readability):

compute combination =
  item1 +
  item2 * 10 +
  item3 * 10**2 +
  item4 * 10**3 +
  item5 * 10**4 +
  item6 * 10**5 +
  item7 * 10**6
.
frequencies combination.

But then I thought, what if he has to do this again sometime with more than 7 digits?  And I thought this would be better:

compute combination = 0.
do repeat item = item1 to item7 / p = 0 to 6.
. compute combination = combination + item * 10**p.
end repeat.
frequencies combination.

HTH.


Eric Black wrote
I have a list of 7 dummy variables and I want to have one variable with all
combinations - in the binary format, i.e. 0010000  means that only the third
dummy is a 1 and all others are 0.
When I do this with the command below, I do get my results but some
combinations yield some odd decimal issue (i.e. case ID 3 is
1001.0000000000001 instead of 0001001)

*Begin***.
data list list / id item1  item2 item3 item4 item5 item6 item7 (8f2.0).
begin data
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 1
3 0 0 0 0 0 1 0
4 1 0 0 1 0 0 0
end data.


numeric combination (N7.0).
      compute combination= 1000000*item7 +
                                                100000*item6 +
                                               10000*item5 +
                                               1000*item4 +
                                               100*item3 +
                                               10*item2 +
                                               item1.

fre combination.
*End***.

This will give me the following:

combination
  Frequency Percent Valid Percent Cumulative Percent
Valid 0000000 1 25.0 25.0 25.0
 1001.0000000000001 1 25.0 25.0 50.0
 0100000 1 25.0 25.0 75.0
 1000000 1 25.0 25.0 100.0
 Total 4 100.0 100.0

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Eric Black
In reply to this post by Eric Black
Thanks Bruce and Stan

It does seem to be an SPSS 21 issue. When I run my version and Bruce's
second in SPSS 20 (on a Win7, 64bit) it works fine while both cause the
problem in SPSS 21.

So the bottomline is to revert back to SPSS 20?

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

David Marso
Administrator
MAYBE after the computation:

FORMAT combination (N7).

Maybe something odd with 'stickiness' of (N7) in NUMERIC declaration?
Eric Black wrote
Thanks Bruce and Stan

It does seem to be an SPSS 21 issue. When I run my version and Bruce's
second in SPSS 20 (on a Win7, 64bit) it works fine while both cause the
problem in SPSS 21.

So the bottomline is to revert back to SPSS 20?

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Eric Black
In reply to this post by Eric Black
Tried that too but as soon as I declare the N7 things get screwed up with v21

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Stan Gorodenski
In reply to this post by Eric Black
> Thanks Bruce and Stan
>
> It does seem to be an SPSS 21 issue. When I run my version and Bruce's
> second in SPSS 20 (on a Win7, 64bit) it works fine while both cause the
> problem in SPSS 21.
>
> So the bottomline is to revert back to SPSS 20?

Or demand a healthy rebate on the cost of the package?  (-:
Stan

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

David Marso
Administrator
In reply to this post by Eric Black
Well, hard to say without the output ;-(
OTOH: here are a couple more approaches.
--
Using Dino 11.5
--
*Begin***.
data list list / id item1  item2 item3 item4 item5 item6 item7 (8f2.0).
begin data
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 1
3 0 0 0 0 0 1 0
4 1 0 0 1 0 0 0
end data.

STRING comb (A7).
DO REPEAT V=item1 to item7.
COMPUTE comb=CONCAT(LTRIM(RTRIM(STRING(V,F1))),RTRIM(comb)).
END REPEAT.
COMPUTE Ncomb=NUMBER(comb,N7).
FORMAT NComb(N7).
LIST.

ID ITEM1 ITEM2 ITEM3 ITEM4 ITEM5 ITEM6 ITEM7 COMB      NCOMB

 1    0     0     0     0     0     0     0  0000000 0000000
 2    0     0     0     0     0     0     1  1000000 1000000
 3    0     0     0     0     0     1     0  0100000 0100000
 4    1     0     0     1     0     0     0  0001001 0001001


Number of cases read:  4    Number of cases listed:  4
Eric Black wrote
Tried that too but as soon as I declare the N7 things get screwed up with v21

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

David Marso
Administrator
Or slightly more natural/jugular?
There are actually any number of decent ways to do this sort of calc.
You could also apply a TRUNC to the accumulated sum of 10^x prior to N7 format.
Why 21 does the alien digit thing is not immediately obvious.  
I see NO reason why 10^x should end up with fuzz!
Maybe someone with 21 can nail it.  
--
*Begin***.
data list list / id item1  item2 item3 item4 item5 item6 item7 (8(A1,X)).
begin data
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 1
3 0 0 0 0 0 1 0
4 1 0 0 1 0 0 0
end data.
LIST.
STRING comb (A7).
DO REPEAT V=item1 to item7.
COMPUTE comb=CONCAT(V,RTRIM(comb)).
END REPEAT.
COMPUTE Ncomb=NUMBER(comb,N7).
FORMAT NComb(N7).
LIST.

David Marso wrote
Well, hard to say without the output ;-(
OTOH: here are a couple more approaches.
--
Using Dino 11.5
--
*Begin***.
data list list / id item1  item2 item3 item4 item5 item6 item7 (8f2.0).
begin data
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 1
3 0 0 0 0 0 1 0
4 1 0 0 1 0 0 0
end data.

STRING comb (A7).
DO REPEAT V=item1 to item7.
COMPUTE comb=CONCAT(LTRIM(RTRIM(STRING(V,F1))),RTRIM(comb)).
END REPEAT.
COMPUTE Ncomb=NUMBER(comb,N7).
FORMAT NComb(N7).
LIST.

ID ITEM1 ITEM2 ITEM3 ITEM4 ITEM5 ITEM6 ITEM7 COMB      NCOMB

 1    0     0     0     0     0     0     0  0000000 0000000
 2    0     0     0     0     0     0     1  1000000 1000000
 3    0     0     0     0     0     1     0  0100000 0100000
 4    1     0     0     1     0     0     0  0001001 0001001


Number of cases read:  4    Number of cases listed:  4
Eric Black wrote
Tried that too but as soon as I declare the N7 things get screwed up with v21

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

Richard Ristow
At 12:49 AM 2/7/2013, David Marso wrote:

>You could also apply a TRUNC to the accumulated sum of 10^x prior to N7
>format.

Probably should use RND instead of TRUNC. This time the calculated
value was just above the correct value; but it's about as likely, in
such cases, that it'll come out just below.

The reported behavior makes no sense to me, either, but I also don't
have a copy of v21 to test on.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Decimals when Creating "Binary" Variable

David Marso
Administrator
Good catch Richard!
--
Richard Ristow wrote
At 12:49 AM 2/7/2013, David Marso wrote:

>You could also apply a TRUNC to the accumulated sum of 10^x prior to N7
>format.

Probably should use RND instead of TRUNC. This time the calculated
value was just above the correct value; but it's about as likely, in
such cases, that it'll come out just below.

The reported behavior makes no sense to me, either, but I also don't
have a copy of v21 to test on.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"