# Thread: spss syntax for a new variable

1. ## spss syntax for a new variable

Hi all,
I am new to SPSS-Syntax. I will appreciate if any of you can help me with the following programming.
I have a variable 'group' with 3 values 1 2 3. I would like to compute another variable 'group2'
from 'group' with another condition on another variable 'age'. The condition is:
I want to keep the first value of 'group' as it is if age <=5 else the value should be changed to 0.
The second value (2) of the 'group' should be as it is if age=6 else 2 should be set to zero. The 3rd value (3) of the 'group' should be as it is if age=7 else 3 should be set to zero. How do I write the SPSS-syntax for this task?

Thank you.
soma

2. ## Re: spss syntax for a new variable

here you are:

Code:
compute group2 = 0.
if (group eq 1) and (age le 5) group2 = group.
if (group eq 2) and (age eq 6) group2 = group.
if (group eq 3) and (age eq 7) group2 = group.
exe.

3. ## Re: spss syntax for a new variable

Originally Posted by helicon
here you are:

Code:
compute group2 = 0.
if (group eq 1) and (age le 5) group2 = group.
if (group eq 2) and (age eq 6) group2 = group.
if (group eq 3) and (age eq 7) group2 = group.
exe.
Thank you very much. I calculated sum of 3 variables by using "compute sum_var=sum(var1+var2+var3)"
I would like to appear the same value of sum_var 3 times or in another variable.
How can do I do that? Thanks again.

4. ## Re: spss syntax for a new variable

I'm not sure I quite understand what you want to do. Do you want a new variable that is three times the value of sum_var or do you want three new variables that are equal to sum_var? Or something else entirely?

Also, your command "compute sum_var=sum(var1+var2+var3)." will result in missing values in any case containing missing data amongst your summed variables. If this is not what you want, use "compute sum_var = sum(var1, var2, var3)."

5. ## Re: spss syntax for a new variable

Originally Posted by helicon
I'm not sure I quite understand what you want to do. Do you want a new variable that is three times the value of sum_var or do you want three new variables that are equal to sum_var? Or something else entirely?

Also, your command "compute sum_var=sum(var1+var2+var3)." will result in missing values in any case containing missing data amongst your summed variables. If this is not what you want, use "compute sum_var = sum(var1, var2, var3)."
Thanks for your reply. Let's say var1=10, var2=20, var3=30. I calculted a new variable sum_var=60.
I would like to appear this value 60 into another varible, say sum_var2, 3 times, i.e., 60, 60, 60. (in 3 rows).
Thanks.

6. ## Re: spss syntax for a new variable

OK, I'm puzzled as to why you would want to do this but here you go:

Code:
string #x (a10).
string sum_var2 (a20).
compute #x=ltrim(string(sum_var,f8)).
compute sum_var2=concat(rtrim(#x), ', ', rtrim(#x), ', ', rtrim(#x)).
exe.

7. ## Re: spss syntax for a new variable

Originally Posted by helicon
OK, I'm puzzled as to why you would want to do this but here you go:

Code:
string #x (a10).
string sum_var2 (a20).
compute #x=ltrim(string(sum_var,f8)).
compute sum_var2=concat(rtrim(#x), ', ', rtrim(#x), ', ', rtrim(#x)).
exe.
Hi,
Thank you so much for your support. I wrote in my previous post that I would like to get the value (60) in three rows (top to bottom); NOT in a single cell as your script does. I would like them as numeric values.
Thank you once again for your support.

8. ## Re: spss syntax for a new variable

Try:

Code:
compute sortid =\$casenum.
add files / file=* / file=* / file=*.
sort cases by sortid.
delete variables sortid.
exe.
If this is not what you need, then you will need to post an example of how your datafile looks now and how you want it to look.

9. ## Re: spss syntax for a new variable

Originally Posted by helicon
Try:

Code:
compute sortid =\$casenum.
add files / file=* / file=* / file=*.
sort cases by sortid.
delete variables sortid.
exe.
If this is not what you need, then you will need to post an example of how your datafile looks now and how you want it to look.
Hi,
Thanks for your reply. I have attached a picture where you can see that I calculated
compute Kontaktfähigkeit_Summe=SUM(Q1, Q2, Q3, Q4, Q5, Q6) =22. I would like this value 22 to appear 2 times more below (in the pink colored 2 cells). Thanks

10. ## Re: spss syntax for a new variable

Here you are:

Code:
if missing(Kontaktfähigkeit_Summe) Kontaktfähigkeit_Summe = lag(Kontaktfähigkeit_Summe).
exe.

11. ## Re: spss syntax for a new variable

Thank you very much! It works!

12. ## Re: spss syntax for a new variable

Hi Helicon,
I wrote the following code to do the following: I want to compute the variable J_Gruppe_Final from J_Gruppe_Cal (please see the attached picture)
after imposing these conditions:
I want to keep the first value of ' J_Gruppe_Cal' as it is if age_year <= 4 else the value should be changed to 0.
The second value of the ' J_Gruppe_Cal' should be as it is if age_year =5 else should be set to zero. The 3rd value of the 'group' should be as it is if age=7 else 3 should be set to zero. I tried the following...but it does not work -- I understand why.

compute J_Gruppe_Final = 0.
do if (age_year le 4).
-compute J_Gruppe_Final = J_Gruppe_Cal.
else if (age_year eq 5).
-compute J_Gruppe_Final = J_Gruppe_Cal.
else if (age_year eq 6).
-compute J_Gruppe_Final = J_Gruppe_Cal.
end if.
EXECUTE.

How do I write the SPSS-syntax for this task?

Thank you.
soma

13. ## Re: spss syntax for a new variable

Your syntax is conditional upon just age, not the combination of age and group.

Not tested but it should look something like this:

Code:
compute j_gruppe_final = 0.
if ((j_gruppe_cal eq 1) and (age_year le 4))
or ((j_gruppe_cal eq 2) and (age_year eq 5))
or ((j_gruppe_cal eq 3) and (age_year eq 6)) j_gruppe_final = j_gruppe_cal.
exe.

14. ## Re: spss syntax for a new variable

Originally Posted by helicon
Your syntax is conditional upon just age, not the combination of age and group.

Not tested but it should look something like this:

Code:
compute j_gruppe_final = 0.
if ((j_gruppe_cal eq 1) and (age_year le 4))
or ((j_gruppe_cal eq 2) and (age_year eq 5))
or ((j_gruppe_cal eq 3) and (age_year eq 6)) j_gruppe_final = j_gruppe_cal.
exe.
Hi,
Thanks for your prompt response. The values of the variable j_gruppe_cal changes. It has a maximum value though.I can use it in your code. What if I do not know the maximum value? How do I change the script then?
Thank you once again for your kind support.

Hi,