Multiplying by a constant

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

Multiplying by a constant

Anthony James
Dear folks,
I want to multiply all the values in the cells of a SPSS spreadsheet by 2.
What should I do?
 
Thanks
Anthony

Reply | Threaded
Open this post in threaded view
|

Re: Multiplying by a constant

J. R. Carroll
Anthony,

Do you mean, EVERY value (aka all rows, all columns)?

Or do you just mean you have a variable you wish to multiple by a constant of 2?  If you want the latter, then:


COMPUTE var2 = var1 * 2.
EXECUTE.


Where "var2" is the name of the new column/variable you want to create with the new values (never delete or overwrite existing data - always make a new column just for safe measure!).  "var1" is the name of the original (or old) variable you wish to multiple by 2.

If you want the former (every single value, across all variables, rows, and columns) then I would think you would need a separate compute statement for each variable (similar to the one above).

Let me know if I missed the mark on this one, and hth,


J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Wed, Mar 2, 2011 at 11:42 PM, Anthony James <[hidden email]> wrote:
Dear folks,
I want to multiply all the values in the cells of a SPSS spreadsheet by 2.
What should I do?
 
Thanks
Anthony


Reply | Threaded
Open this post in threaded view
|

Re: Multiplying by a constant

John F Hall

..or you could do:

 

Do repeat.

X = v1 to vn.

Compute x = x*2.

End repeat.

 

...but why are you doing this?

 

I once caught a major survey company out by checking the raw data their client had paid for.  The client asked for double sampling of young people, but the agency simply duplicated all the young cases.  Very naughty: I hope you’re not doing anything like that!

 

 

John Hall

[hidden email]

www.surveyresearch.weebly.com

 

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Justin Carroll
Sent: 03 March 2011 09:30
To: [hidden email]
Subject: Re: Multiplying by a constant

 

Anthony,

Do you mean, EVERY value (aka all rows, all columns)?

Or do you just mean you have a variable you wish to multiple by a constant of 2?  If you want the latter, then:


COMPUTE var2 = var1 * 2.
EXECUTE.


Where "var2" is the name of the new column/variable you want to create with the new values (never delete or overwrite existing data - always make a new column just for safe measure!).  "var1" is the name of the original (or old) variable you wish to multiple by 2.

If you want the former (every single value, across all variables, rows, and columns) then I would think you would need a separate compute statement for each variable (similar to the one above).

Let me know if I missed the mark on this one, and hth,


J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204

On Wed, Mar 2, 2011 at 11:42 PM, Anthony James <[hidden email]> wrote:

Dear folks,

I want to multiply all the values in the cells of a SPSS spreadsheet by 2.

What should I do?

 

Thanks

Anthony

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Multiplying by a constant

John F Hall
In reply to this post by Anthony James

My solution had a small error.  It should have been

 

do repeat x=v1 to v2.

compute x=2*x.

end repeat.

 

Thanks Ruben for noticing this: as Bruce always says RTFM.

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Anthony James
Sent: 03 March 2011 08:43
To: [hidden email]
Subject: Multiplying by a constant

 

Dear folks,

I want to multiply all the values in the cells of a SPSS spreadsheet by 2.

What should I do?

 

Thanks

Anthony

 

Reply | Threaded
Open this post in threaded view
|

Re: Multiplying by a constant

drfg2008
In reply to this post by Anthony James
I don't think syntax (above will work). You have 2 options:

1. you can use a macro.
2. you can use python.

If you're not familiar with python, the following syntax defenitely works, but only if the variables are all named in a special order: v1, v2, v3, ..., v100, v101, ..., vn.

If it's not like that, you need a program, that is capable to read the variable names. That's python. Bu if you don't know how to deal with python, it's a too problematic way. So here is the macro syntax for a file that is ordered as I said above. Copy and paste an run it.

Frank


input program.
loop a =1 to 100 by 1.
end case.
end loop.
end file.
end input program.
exe.

COMP v1=RV.NORMAL(1,10).
COMP v2=RV.NORMAL(1,10).
COMP v3=RV.NORMAL(1,10).
COMP v4=RV.NORMAL(1,10).
EXECUTE .


* only the variables in the file start: v1, v2, ..., vn.
* if variables have different names, use python !!!.

DEFINE  !makro_name (start =!tokens(1)
              /end = !tokens(1) ).
  !do !var = !start !to !end.
    compute !concat(v_, !var) = !concat(v, !var) *2 .
  !doend.
EXECUTE .
!enddefine.

!makro_name start = 1 end = 4.

DELETE VARIABLES v1 to v4.
Dr. Frank Gaeth