Loop Programming to Add Random Numbers

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

Loop Programming to Add Random Numbers

David Curry
Dear Merciful Experienced SPSS Programmers,

I have multiple lines of data per respondent as shown in the sample file
below.  All I want to do is to add a 3rd variable to these data that
contains a uniform random number by respondent (not by line). I am new to
SPSS, hence SPSS's notion of loops, vectors, data types (is ordinal =
integer or what?) is new to me. (Venting here ... in Matlab this is a
cinch.)
I would appreciate help on this and thank whomever does so.


Rid     Var1
1.00    a
1.00    a
1.00    a
2.00    b
2.00    b
2.00    b
3.00    c
3.00    c
4.00    d
4.00    d
4.00    d
4.00    d


The file should end up like this.  Respondent 1 has the same random
no. .1234 associated with all lines of data for him or her; and so on.
Rid     V1      RandNo
1.00    a       .1234
1.00    a       .1234
1.00    a       .1234
2.00    b       .6789
2.00    b       .6789
2.00    b       .6789
3.00    c       .2468
3.00    c       .2468
4.00    d       .3579
4.00    d       .3579
4.00    d       .3579
4.00    d       .3579

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Loop Programming to Add Random Numbers

Jon K Peck

There are many ways to do this.  Here is a simple one.  The dataset does not need to be sorted to use this.
compute rand = rv.uniform(0,1).
AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /BREAK=Rid   /rand_first=FIRST(rand).

HTH,
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: David Curry <[hidden email]>
To: [hidden email]
Date: 09/15/2010 10:48 AM
Subject: [SPSSX-L] Loop Programming to Add Random Numbers
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear Merciful Experienced SPSS Programmers,

I have multiple lines of data per respondent as shown in the sample file
below.  All I want to do is to add a 3rd variable to these data that
contains a uniform random number by respondent (not by line). I am new to
SPSS, hence SPSS's notion of loops, vectors, data types (is ordinal =
integer or what?) is new to me. (Venting here ... in Matlab this is a
cinch.)
I would appreciate help on this and thank whomever does so.


Rid     Var1
1.00    a
1.00    a
1.00    a
2.00    b
2.00    b
2.00    b
3.00    c
3.00    c
4.00    d
4.00    d
4.00    d
4.00    d


The file should end up like this.  Respondent 1 has the same random
no. .1234 associated with all lines of data for him or her; and so on.
Rid     V1      RandNo
1.00    a       .1234
1.00    a       .1234
1.00    a       .1234
2.00    b       .6789
2.00    b       .6789
2.00    b       .6789
3.00    c       .2468
3.00    c       .2468
4.00    d       .3579
4.00    d       .3579
4.00    d       .3579
4.00    d       .3579

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD