How to create a SUPER Variable

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

How to create a SUPER Variable

Eugenio Grant
Hi:



I have a DataBase of 2500 variables, all are numbers.



V1 to V2500, and I need to create the a 200 character variable that
concatenates such values like this..



If V1 = 1 and V2 = 7 and V3 = 9 I need a new variable like this.



Vnew = 179 and so on.



Can I create 200 character variable like this, does SPSS has a limitation on
the length of a variable?.



Regards,
Reply | Threaded
Open this post in threaded view
|

Re: How to create a SUPER Variable

Hal 9000
What about something like this? :




new file.
data list free /v1 v2 v3.
begin data
1 3 5
2 4 6
end data.

string SUPER (a200).
do repeat #T = v1 to v3.
compute SUPER = concat(rtrim(SUPER), rtrim(string(#T, f1))).
end repeat.
exe.
-Gary



On 1/18/07, Eugenio Grant <[hidden email]> wrote:

> Hi:
>
>
>
> I have a DataBase of 2500 variables, all are numbers.
>
>
>
> V1 to V2500, and I need to create the a 200 character variable that
> concatenates such values like this..
>
>
>
> If V1 = 1 and V2 = 7 and V3 = 9 I need a new variable like this.
>
>
>
> Vnew = 179 and so on.
>
>
>
> Can I create 200 character variable like this, does SPSS has a limitation
> on
> the length of a variable?.
>
>
>
> Regards,
>
Reply | Threaded
Open this post in threaded view
|

Re: How to create a SUPER Variable

Dennis Deck
In reply to this post by Eugenio Grant
Issues:
 - It is not clear *why* you want to do this.
 - I'm not sure how you plan to store 2500 values in a 200 character
variable (so I think you have left out something in your description).
 - I assume the variables are all single digit (0-9).
 - Missing data would be another complication.

Some options:

a) With a small number of variables you could do something like:
COMPUTE vnew = v1*100 + v2*10 + v3

b) But with 200 then try something like (untested):
STRING vnew (A200) .
DO REPEAT v = V1 to V200
+ COMPUTE vnew = CONCAT(vnew,STRING(v)) .
END REPEAT .

c) Alternatively consider using WRITE and WRITE FORMATS to output a
fixed length ASCII record.

-----Original Message-----
From: Eugenio Grant [mailto:[hidden email]]
Sent: Thursday, January 18, 2007 4:09 PM
Subject: How to create a SUPER Variable

Hi:



I have a DataBase of 2500 variables, all are numbers.



V1 to V2500, and I need to create the a 200 character variable that
concatenates such values like this..



If V1 = 1 and V2 = 7 and V3 = 9 I need a new variable like this.



Vnew = 179 and so on.



Can I create 200 character variable like this, does SPSS has a
limitation on
the length of a variable?.



Regards,
Reply | Threaded
Open this post in threaded view
|

Re: How to create a SUPER Variable

Richard Ristow
In reply to this post by Eugenio Grant
A couple of comments. At 07:08 PM 1/18/2007, Eugenio Grant wrote:

>>I have a DataBase of 2500 variables, all are numbers, V1 to V2500,
>>and I need to create the a 200 character variable that concatenates
>>such values like this..
>>
>>If V1 = 1 and V2 = 7 and V3 = 9 I need a new variable like this.
>>
>>Vnew = 179 and so on.

First, as Dennis Deck remarked, "I'm not sure how you plan to store
2500 values in a 200 character variable".

You may not have planned to use a 200-character variable. You asked,

>>Can I create 200 character variable like this, does SPSS has a
>>limitation on
>>the length of a variable?.

The length of an SPSS variable used to be limited to 255 characters,
and may now (Release 14 and beyond) be up to 32,767 characters. I don't
know when the limit was increased, but I suspect it was in Release 14.

You've had two suggestions for code. At 01:14 AM 1/19/2007, Hal 9000
wrote:

>string SUPER (a200).
>do repeat #T = v1 to v3.
>.  compute SUPER = concat(rtrim(SUPER), rtrim(string(#T, f1))).
>end repeat.

This works fine (I just tested it).
. The stand-in variable in the DO REPEAT (#T) doesn't have to be a
scratch variable. A stand-in variable isn't a real variable, and won't
clutter the file no matter what is its name.
. The 'rtrim' for SUPER is necessary. The rtrim for "(string(#T,f1)" is
not. Generalizing, you never need to RTRIM the *LAST* argument to
CONCAT. You often need to do so for all other arguments.

And at 03:08 AM 1/19/2007, Dennis Deck wrote:

>b) But with 200 then try something like (untested):
>STRING vnew (A200) .
>DO REPEAT v = V1 to V200
>+ COMPUTE vnew = CONCAT(vnew,STRING(v)) .
>END REPEAT .

Two problems I see. First, something you have to be careful with, with
CONCAT: CONCAT takes the *whole* strings it's given. In this case, the
left argument to CONCAT is the whole 200-character string 'vnew'; the
right argument is catenated beyond the 200th character, and is then
lost. Usually, you need RTRIM on the left argument of CONCAT, as 'Hal
9000' did. And "STRING" requires a numeric format as its second
argument.

A variation, as an illustration of what can be done with SUBSTR,
another way of doing it:
STRING SUPER2(A200).
do repeat VARIABLE = v1 to v3
          /POSITION =  1 to  3.
.  COMPUTE SUBSTR(SUPER2,POSITION,1)
                  = STRING(VARIABLE,F1).
end repeat.

Here's a test/demo of Gary's code ('Hal 9000') and mine. SPSS 15.0.1
draft output <to WRR: code & output not saved separately>. The SUPER
variables are shortened, to fit on one line in the listing.


new file.
data list free /v1 v2 v3.
begin data
1 3 5
2 4 6
end data.
FORMATS v1 v2 v3 (F2).

string SUPER (a15).
do repeat #T = v1 to v3.
.  compute SUPER = concat(rtrim(SUPER),
                           rtrim(string(#T, f1))).
end repeat.

STRING SUPER2(A15).
do repeat VARIABLE = v1 to v3
          /POSITION =  1 to  3.
.  COMPUTE SUBSTR(SUPER2,POSITION,1)
                  = STRING(VARIABLE,F1).
end repeat.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |19-JAN-2007 14:26:57       |
|-----------------------------|---------------------------|

v1 v2 v3 SUPER           SUPER2

  1  3  5 135             135
  2  4  6 246             246

Number of cases read:  2    Number of cases listed:  2
Reply | Threaded
Open this post in threaded view
|

Re: How to create a SUPER Variable

Oliver, Richard
Maximum string width was increased to 32K (32,767 bytes) in release 13.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Richard Ristow
Sent: Friday, January 19, 2007 1:28 PM
To: [hidden email]
Subject: Re: How to create a SUPER Variable

A couple of comments. At 07:08 PM 1/18/2007, Eugenio Grant wrote:

>>I have a DataBase of 2500 variables, all are numbers, V1 to V2500,
>>and I need to create the a 200 character variable that concatenates
>>such values like this..
>>
>>If V1 = 1 and V2 = 7 and V3 = 9 I need a new variable like this.
>>
>>Vnew = 179 and so on.

First, as Dennis Deck remarked, "I'm not sure how you plan to store
2500 values in a 200 character variable".

You may not have planned to use a 200-character variable. You asked,

>>Can I create 200 character variable like this, does SPSS has a
>>limitation on
>>the length of a variable?.

The length of an SPSS variable used to be limited to 255 characters,
and may now (Release 14 and beyond) be up to 32,767 characters. I don't
know when the limit was increased, but I suspect it was in Release 14.

You've had two suggestions for code. At 01:14 AM 1/19/2007, Hal 9000
wrote:

>string SUPER (a200).
>do repeat #T = v1 to v3.
>.  compute SUPER = concat(rtrim(SUPER), rtrim(string(#T, f1))).
>end repeat.

This works fine (I just tested it).
. The stand-in variable in the DO REPEAT (#T) doesn't have to be a
scratch variable. A stand-in variable isn't a real variable, and won't
clutter the file no matter what is its name.
. The 'rtrim' for SUPER is necessary. The rtrim for "(string(#T,f1)" is
not. Generalizing, you never need to RTRIM the *LAST* argument to
CONCAT. You often need to do so for all other arguments.

And at 03:08 AM 1/19/2007, Dennis Deck wrote:

>b) But with 200 then try something like (untested):
>STRING vnew (A200) .
>DO REPEAT v = V1 to V200
>+ COMPUTE vnew = CONCAT(vnew,STRING(v)) .
>END REPEAT .

Two problems I see. First, something you have to be careful with, with
CONCAT: CONCAT takes the *whole* strings it's given. In this case, the
left argument to CONCAT is the whole 200-character string 'vnew'; the
right argument is catenated beyond the 200th character, and is then
lost. Usually, you need RTRIM on the left argument of CONCAT, as 'Hal
9000' did. And "STRING" requires a numeric format as its second
argument.

A variation, as an illustration of what can be done with SUBSTR,
another way of doing it:
STRING SUPER2(A200).
do repeat VARIABLE = v1 to v3
          /POSITION =  1 to  3.
.  COMPUTE SUBSTR(SUPER2,POSITION,1)
                  = STRING(VARIABLE,F1).
end repeat.

Here's a test/demo of Gary's code ('Hal 9000') and mine. SPSS 15.0.1
draft output <to WRR: code & output not saved separately>. The SUPER
variables are shortened, to fit on one line in the listing.


new file.
data list free /v1 v2 v3.
begin data
1 3 5
2 4 6
end data.
FORMATS v1 v2 v3 (F2).

string SUPER (a15).
do repeat #T = v1 to v3.
.  compute SUPER = concat(rtrim(SUPER),
                           rtrim(string(#T, f1))).
end repeat.

STRING SUPER2(A15).
do repeat VARIABLE = v1 to v3
          /POSITION =  1 to  3.
.  COMPUTE SUBSTR(SUPER2,POSITION,1)
                  = STRING(VARIABLE,F1).
end repeat.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |19-JAN-2007 14:26:57       |
|-----------------------------|---------------------------|

v1 v2 v3 SUPER           SUPER2

  1  3  5 135             135
  2  4  6 246             246

Number of cases read:  2    Number of cases listed:  2