SAMPLE without a number as limit?

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

SAMPLE without a number as limit?

Robert L

I have used the SAMPLE command to draw a random sample, and it is of course easy to find the number of cases and use this number in the command (“SAMPLE 500 FROM 182828”). However, it would be convenient to set this upper limit with a variable. I thought the following would work:

 

AGGREGATE

  /OUTFILE=* MODE=ADDVARIABLES

  /BREAK=

  /nobs=N.

 

FILTER OFF.

USE ALL.

SAMPLE  400 FROM nobs.

 

But it doesn’t work, it seems as if nobs must be specified as a number. Any workarounds?

 

Robert

===================== 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
Robert Lundqvist
Reply | Threaded
Open this post in threaded view
|

Re: SAMPLE without a number as limit?

Jon Peck
In your code, nobs is a casewise variable, not a scalar, so it is not appropriate as a parameter to SAMPLE.  You can, however, do this using a bit of Python code.

Run this block once in your session to define a little function named sample.

begin program.
import spssaux
def sample(n):
    N = spssaux.getShow("N")
    spss.Submit("""FILTER OFF.
    USE ALL.
    SAMPLE  %(n)s FROM %(N)s.""" % locals())
end program.

Then to actually draw the sample, use this code.

begin program.
sample(100)
end program.

If the data have not been previously passed in the session by running any procedure and the source is not a sav file, the N may be unknown, and the function will fail, but as long as N is known in the Statistics backend, it will work.



On Wed, May 13, 2020 at 2:18 AM Robert Lundqvist <[hidden email]> wrote:

I have used the SAMPLE command to draw a random sample, and it is of course easy to find the number of cases and use this number in the command (“SAMPLE 500 FROM 182828”). However, it would be convenient to set this upper limit with a variable. I thought the following would work:

 

AGGREGATE

  /OUTFILE=* MODE=ADDVARIABLES

  /BREAK=

  /nobs=N.

 

FILTER OFF.

USE ALL.

SAMPLE  400 FROM nobs.

 

But it doesn’t work, it seems as if nobs must be specified as a number. Any workarounds?

 

Robert

===================== 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


--
Jon K Peck
[hidden email]

===================== 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: SAMPLE without a number as limit?

Bruce Weaver
Administrator
In reply to this post by Robert L
One rarely has the opportunity to make Jon and David (Marso) both cringe at
the same time, so I feel compelled to point out that David's (ginger-haired)
"horrible hack" approach could also be used here.  E.g.,

* Modify the FILE HANDLE commands as needed.
* First file handle points to where SPSS sample datasets are stored.
FILE HANDLE SPSSdata /NAME="C:/SPSSdata".
* Second file handle points to a temp folder you can write to.
FILE HANDLE Temp /NAME="C:/Temp".
* The rest of the code should run as is.

NEW FILE.
DATASET CLOSE ALL.
GET FILE "SPSSdata/survey_sample.sav".

AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /BREAK=
  /nobs=N.

FORMATS nobs (F8.0).
DESCRIPTIVES nobs.

* Here's where the "horrible hack" starts.
* Use WRITE to write a syntax file that
* contains a macro definition for a macro that
* returns the value of nobs.
DO IF $casenum=1.
WRITE out="Temp/hh.sps"
 /"DEFINE !nobs() " nobs " !ENDDEFINE.".
END IF.
EXECUTE.
* Now use INSERT to run the macro definition.
INSERT FILE = "Temp/hh.sps".

* And finally, use the macro in SAMPLE.
FILTER OFF.
USE ALL.
SAMPLE  400 FROM !nobs.

* Verify that there are 400 cases in the file.
DESCRIPTIVES nobs.

* Clean up in Aisle 6:  Delete the sps file.
ERASE FILE = "Temp/hh.sps".


QED.  NPR.  



Robert L wrote

> I have used the SAMPLE command to draw a random sample, and it is of
> course easy to find the number of cases and use this number in the command
> (“SAMPLE 500 FROM 182828”). However, it would be convenient to set this
> upper limit with a variable. I thought the following would work:
>
> AGGREGATE
>   /OUTFILE=* MODE=ADDVARIABLES
>   /BREAK=
>   /nobs=N.
>
> FILTER OFF.
> USE ALL.
> SAMPLE  400 FROM nobs.
>
> But it doesn’t work, it seems as if nobs must be specified as a number.
> Any workarounds?
>
> Robert
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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





-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: SAMPLE without a number as limit?

Jon Peck
Cringe and moan!  I'd call that SPSS malpractice :-(

On Wed, May 13, 2020 at 8:53 AM Bruce Weaver <[hidden email]> wrote:
One rarely has the opportunity to make Jon and David (Marso) both cringe at
the same time, so I feel compelled to point out that David's (ginger-haired)
"horrible hack" approach could also be used here.  E.g.,

* Modify the FILE HANDLE commands as needed.
* First file handle points to where SPSS sample datasets are stored.
FILE HANDLE SPSSdata /NAME="C:/SPSSdata".
* Second file handle points to a temp folder you can write to.
FILE HANDLE Temp /NAME="C:/Temp".
* The rest of the code should run as is.

NEW FILE.
DATASET CLOSE ALL.
GET FILE "SPSSdata/survey_sample.sav".

AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /BREAK=
  /nobs=N.

FORMATS nobs (F8.0).
DESCRIPTIVES nobs.

* Here's where the "horrible hack" starts.
* Use WRITE to write a syntax file that
* contains a macro definition for a macro that
* returns the value of nobs.
DO IF $casenum=1.
WRITE out="Temp/hh.sps"
 /"DEFINE !nobs() " nobs " !ENDDEFINE.".
END IF.
EXECUTE.
* Now use INSERT to run the macro definition.
INSERT FILE = "Temp/hh.sps".

* And finally, use the macro in SAMPLE.
FILTER OFF.
USE ALL.
SAMPLE  400 FROM !nobs.

* Verify that there are 400 cases in the file.
DESCRIPTIVES nobs.

* Clean up in Aisle 6:  Delete the sps file.
ERASE FILE = "Temp/hh.sps".


QED.  NPR. 



Robert L wrote
> I have used the SAMPLE command to draw a random sample, and it is of
> course easy to find the number of cases and use this number in the command
> (“SAMPLE 500 FROM 182828”). However, it would be convenient to set this
> upper limit with a variable. I thought the following would work:
>
> AGGREGATE
>   /OUTFILE=* MODE=ADDVARIABLES
>   /BREAK=
>   /nobs=N.
>
> FILTER OFF.
> USE ALL.
> SAMPLE  400 FROM nobs.
>
> But it doesn’t work, it seems as if nobs must be specified as a number.
> Any workarounds?
>
> Robert
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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





-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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


--
Jon K Peck
[hidden email]

===================== 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