Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copying/recoding STRING variables into new variable

5,780 views
Skip to first unread message

Rene

unread,
Sep 15, 2011, 4:12:01 AM9/15/11
to
Dear group members,

I have the following problem: I have a main DV (string), which is
divided onto four different SPSS variables (one for each experimental
condition). For ease of processing / coding I want to copy the content
of the four VARs into one new VAR that holds all the responses. When
using the normal COMPUTATION command, with or without IF option, I
receive an error message stating that I can't combine string and
numeric VARs.

Here is what I tried:
IF (A = 1 AND B = 1) NEWVAR=VAR1.
IF (A = -1 AND B = 1) NEWVAR=VAR2.
IF (A = -1 AND B = -1) NEWVAR=VAR3.
IF (A = 1 AND B = -1) NEWVAR=VAR4.

How else can I copy the contents from one string VAR into a new VAR?

Thanks in advance,
Rene

Rene

unread,
Sep 15, 2011, 4:53:39 AM9/15/11
to
OK, I probably should have used the search first. Here is what I
found:

STRING newvar(A100).
COMPUTE NEWVAR=CONCAT(oldvar1,oldvar2,oldvar3,oldvar4).

Now, the thing is I do not REALLY want to 'combine' since three out of
each four variables are missing (because of my experimental
conditions).
So the typical set up would look like this:
Case 1 'blablabla some stuff people write' '-66' '-66' '-66'
Case 2 '-66' '-66' '-66' 'blablabla some stuff people write'
Case 3 '-66' '-66' 'blablabla some stuff people write' '-66'
Case 4 '-66' 'blablabla some stuff people write' '-66' '-66'

So my question is: Can I combine the IF (A=1 AND B=1) command with the
concat command or do I need to somehow tweak the concate command to
omit the -66?

Thanks!

Art Kendall

unread,
Sep 15, 2011, 9:51:54 AM9/15/11
to
I don't see why you need the CONCAT function, just the assignment operator ("=")
see if something like this untested syntax works.

*check whether every case has value for A and B.
crosstabs tables = A by B/missing=include.
STRING newvar(A100).
IF (A EQ 1 AND B EQ 1) NEWVAR=VAR1.
IF (A EQ -1 AND B EQ 1) NEWVAR=VAR2.
IF (A EQ -1 AND B EQ -1) NEWVAR=VAR3.
IF (A EQ 1 AND B EQ -1) NEWVAR=VAR4.

P.S. Are you sure that the 4 "cases" you mention are the only ones that
do in fact occur in the data?

Art Kendall
Social research Consultants

Andy W

unread,
Sep 15, 2011, 10:05:58 AM9/15/11
to
You could do that, but if all of your missing variable strings are
coded as "-66", I would just make a new set of variables, recode the
"-66" to a blank string, and then concatenate the variables all
together. Below is an example.

**********************************.
data list free ("|") / oldvar1 TO oldvar4 (4A10).
begin data
blablabla|-66|-66|-66
-66|-66|-66|blablabla
-66|-66|blablabla |-66
-66|blablabla|-66|-66
end data.

vector oldvarR(4,A10).
recode oldvar1 to oldvar4 ("-66" = "")(ELSE = COPY) into oldvarR1 to
oldvarR4.
execute.

*the use of RTRIM makes it so any blank spaces are removed in the
empty strings.
string newvar (A40).
compute newvar =
CONCAT(RTRIM(oldvarR1),RTRIM(oldvarR2),RTRIM(oldvarR3),RTRIM(oldvarR4)).
execute.
*************************************.

If your missing value codes aren't all "-66" this wouldn't work as
well. You could do it using if statements as well, although it the
stars align correctly in your example this may be slightly less work.

Andy W

Andy W

unread,
Sep 15, 2011, 10:23:30 AM9/15/11
to
I see now that 3 out of the 4 are always missing! If that is the case
Art's suggestion makes more sense. My suggestion would work though if
you need to concatenate more strings in the case that 3 out of the 4
are not always missing.

Rene

unread,
Sep 15, 2011, 10:25:00 AM9/15/11
to
Thanks Art! Worked perfectly!

Rene

unread,
Sep 15, 2011, 10:37:54 AM9/15/11
to
Thanks Art! Worked perfectly!

On Sep 15, 3:51 pm, Art Kendall <A...@DrKendall.org> wrote:

Bruce Weaver

unread,
Sep 15, 2011, 10:45:01 AM9/15/11
to
I think Andy's more general approach could be shortened to something like:

string newvar(a40).
compute newvar = "".
do repeat oldvar = oldvar1 to oldvar4.
- if (oldvar NE "-66") newvar = concat(RTRIM(newvar),RTRIM(oldvar)).
end repeat.


--
Bruce Weaver
bwe...@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."

Art Kendall

unread,
Sep 15, 2011, 10:46:02 AM9/15/11
to
You're welcome.

Art
0 new messages