Copy a string variable

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

alterego

Copy a string variable

Postby alterego » Wed Jun 27, 2012 12:52 pm

OK, and my second problem - not that urgent as I found a workaround, but still puzzling to me.

I just want to copy a string variable into another string variable with a slightly different name. This is my code:

Code: Select all

recode var1 into var2.
Variable var2 does exist - although I thought I had used the code before and it did not only work, but worked without var2 to be defined beforehand.

What I do now is:

Code: Select all

recode var1 ('1'='1') (else=copy) into var2.
Which does the same thing, even if var1 doesn't contain the string '1'.

I just want to know whats wrong with the upper code... Maybe someone had got the same problem and solved it.

Thanks a lot.

alterego
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: Copy a string variable

Postby GerineL » Wed Jun 27, 2012 2:03 pm

I use compute for this (compute var2 = var1).
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: Copy a string variable

Postby Penguin_Knight » Wed Jun 27, 2012 5:09 pm

The recode syntax chart is like this:

Code: Select all

RECODE varlist (value list=value)...(value list=value) [INTO varlist]
       [/varlist...]
You can see that the coding scheme (value list=value)...(value list=value) is not enclosed in a pair of square brackets ([ and ]), this means it's not optional and has to be provided. So, the first syntax:

Code: Select all

recode var1 into var2.
is doomed to fail.

For the second syntax:

Code: Select all

recode var1 ('1'='1') (else=copy) into var2.
Actually ('1'='1') is redundant. If you have defined var2 in advance using

Code: Select all

STRING var2(A8) .
The code will run with or without ('1'='1'). On the contrary, if var2 has not be declared as string before, even with ('1'='1'), the code will not run.
alterego

Re: Copy a string variable

Postby alterego » Fri Jun 29, 2012 1:33 pm

Hello you two again, and thank you very much for your help!
Gutnre wrote:compute var2 = var1.
I tried that at first, but SPSS kept telling me something that I interpreted as the following: You can only create numeric variables with "compute". A string variable has to be defined beforehand. (I can't post it as it is in German.)
Penguin_Knight wrote:On the contrary, if var2 has not be declared as string before, even with ('1'='1'), the code will not run.
Thank you. I didn't know that.

But, actually, there is a problem with both solutions:
If I create the string variable var2 I have got to assign a length. But what if I don't know yet how long var1 will be? I can set the length of var2 to a10000, but there might still be some jerk among my subjects who wants to write a romance in this special question, and then SPSS will chop off the last characters if I try to copy var1 into the limited var2 – in the first solution even whitout a notification!
I just want to copy a string variable – no matter what length the original variable has… That should be possible!

Thanks again for your help,
alterego
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: Copy a string variable

Postby Penguin_Knight » Fri Jun 29, 2012 6:03 pm

1) You will know the length of var1, it's in the variable tab, right next to the variable name.

2) If you mean to use SPSS as a data entry device, then enter the subject ID and the open-ended answers into Excel, followed by importing the Excel file into SPSS. That way the length of the string variables will be adjusted by itself.
alterego

Re: Copy a string variable

Postby alterego » Fri Jun 29, 2012 7:16 pm

Hello Penguin_Knight,

thank you very much for your constant help.

I know I can see the length of the variables. And yes, my data is an imported Excel file. And that's why I'm so anxious: The longest string variable had a length of about 32000 characters!
It's just that I get the data weekly and I wanted my syntax to automatically analyze everything so that I had to do nothing more than to run it over my data and look at the results. This way I always have to check the lengths of the string variables (and there are several of them). It shouldn't be that difficult for a programm like SPSS to just copy a string variable. It can copy a numeric – why not a string? (Well, ok, numerics with 32000 characters are quite rare, but anyway…)

But really: Thanks for your help! I'm now quite sure that what I want is (for some mysterious reason) not possible in SPSS. I should write to them and tell them… Or better still, make a list of bugs and usability failures (: I am certain there are many things you guys could add.

alterego

Who is online

Users browsing this forum: No registered users and 1 guest

cron