typecasting

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

typecasting

besheer mohamed
Does anyone know how to do either of these in one line?
  1) widen a string (say from length 4 to 6)
  2) convert a string to a number.

  I know you can do either of these if you're willing to ...
  a) create a temp variable of the desired type/length b)  copy the data from the old variable to the temp variable c)  delete the old variable d) recreate the old variable as the desired type/length e) copy the data from the temp variable into the recreated variable and f) delete the temp variable...
  but that's insane. Is there an easier way to do either of these actions?

  Thanks,
  Besheer




---------------------------------
Yahoo! Music Unlimited - Access over 1 million songs.Try it free.
Reply | Threaded
Open this post in threaded view
|

Re: typecasting

Keith McCormick
Hello Besheer,

COMPUTE numb = number(VAR1,f2.0) .  that should take care of the second one.

reading between the lines, you might need to add leading zeros, or
spaces so for the first one the solution would also be a function with
COMPUTE, namely LPAD (or maybe RPAD).

Best,

Keith
keithmccormick.com


On 7/6/06, besheer mohamed <[hidden email]> wrote:

> Does anyone know how to do either of these in one line?
>  1) widen a string (say from length 4 to 6)
>  2) convert a string to a number.
>
>  I know you can do either of these if you're willing to ...
>  a) create a temp variable of the desired type/length b)  copy the data from the old variable to the temp variable c)  delete the old variable d) recreate the old variable as the desired type/length e) copy the data from the temp variable into the recreated variable and f) delete the temp variable...
>  but that's insane. Is there an easier way to do either of these actions?
>
>  Thanks,
>  Besheer
>
>
>
>
> ---------------------------------
> Yahoo! Music Unlimited - Access over 1 million songs.Try it free.
>
Reply | Threaded
Open this post in threaded view
|

Macro question

Roberts, Michael
In reply to this post by besheer mohamed
Hi list,

I do not have any macro experience and the spsstools macro tutorial
leaves me cold!  Would anyone be able to suggest a macro approach
outline to the following:

1. Open n of N files
2. Select a subset based on a given criterion
3. Sort by some other criterion
4. Compute a new variable based on two existing vars
5. Aggregate into a new file

TIA

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Macro question

Roberts, Michael
Note: the 'n' is a specific file and not more than 1 file at a time.

TIA

Mike


-----Original Message-----
From: Roberts, Michael
Sent: Tuesday, July 11, 2006 2:32 PM
To: [hidden email]
Subject: Macro question

Hi list,

I do not have any macro experience and the spsstools macro tutorial
leaves me cold!  Would anyone be able to suggest a macro approach
outline to the following:

1. Open n of N files
2. Select a subset based on a given criterion 3. Sort by some other
criterion 4. Compute a new variable based on two existing vars 5.
Aggregate into a new file

TIA

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Macro question

Peck, Jon
In reply to this post by Roberts, Michael
This sounds straightforward if you have SPSS 14 and the programmability plug-in -- no macro required but a little Python knowledge necessary.

-Jon Peck
SPSS

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Roberts, Michael
Sent: Tuesday, July 11, 2006 1:32 PM
To: [hidden email]
Subject: [SPSSX-L] Macro question

Hi list,

I do not have any macro experience and the spsstools macro tutorial
leaves me cold!  Would anyone be able to suggest a macro approach
outline to the following:

1. Open n of N files
2. Select a subset based on a given criterion
3. Sort by some other criterion
4. Compute a new variable based on two existing vars
5. Aggregate into a new file

TIA

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Macro question

Marks, Jim
In reply to this post by Roberts, Michael
Something like this should get you started.

DEFINE !get (fil = !CMDEND).
!DO !var !IN (!fil).

GET FILE = !QUOTE(!CONCAT("C:\data\",!var,".sav")).

SELECT IF yourvar_1 = 001.
SORT CASES BY yourvar_2.
COMPUTE newvar = yourvar_3* yourvar_4.

AGGREGATE OUTFILE =
  !QUOTE(!CONCAT("C:\data\processed_",!var,".sav"))
  /BREAK = yourvar_4
  /newvar= SUM(newvar).

!DOEND
!ENDDEFINE

*** call the macro.

!get fil = n1 do_next last_file.

Notes:
!DO !IN runs through the list ( "fil =  n1 do_next last_file"),
replacing !var with a different value from the list each time. (DO !TO
can also be used for a set of integers).

The!QUOTE(!CONCAT( will create the file name and path as a string with
quotation marks-- edit to match your path.

SPSS procedures are unchanged by the macro. In this case, the same
values and variables will be used for the select if, sort, compute and
break.

The original file is unchanged by this macro (it is transformed but not
saved.)

A more complicated macro can be designed if you want to change things
depending on the file created. Like this:

DEFINE !get (fil = !TOKENS(1)
        /sel = !TOKENS(1)
        /srt = !TOKENS(1)
        /BRK = !TOKENS (1)).

GET FILE = !QUOTE(!CONCAT("C:\data\",!fil,".sav")).

SELECT IF yourvar_1 = !sel.
SORT CASES BY !srt.
COMPUTE newvar = yourvar_3* yourvar_4.

AGGREGATE OUTFILE =
  !QUOTE(!CONCAT("C:\data\processed_",!fil,".sav"))
  /BREAK = !brk
  /newvar= SUM(newvar).

!ENDDEFINE

*** call the macro.

!get fil = n1 sel = 9 srt = yourvar brk = yourvar_2.
!get fil = do_next sel = 5 srt = yourvar_6 brk = var_1.


Note that the !DO and !DOEND are removed, and the macro is called
multiple times. (You have created the looping structure manually.)

The general principle is that the macro will replace the placeholders in
the syntax (e.g.!fil) with the values in the macro call (e.g. n1)

This will most likely take some tinkering to get the exact files to run.
Use a small test file and start with the command

SET MPRINT ON.

Cheers

--jim



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Roberts, Michael
Sent: Tuesday, July 11, 2006 1:38 PM
To: [hidden email]
Subject: Re: Macro question

Note: the 'n' is a specific file and not more than 1 file at a time.

TIA

Mike


-----Original Message-----
From: Roberts, Michael
Sent: Tuesday, July 11, 2006 2:32 PM
To: [hidden email]
Subject: Macro question

Hi list,

I do not have any macro experience and the spsstools macro tutorial
leaves me cold!  Would anyone be able to suggest a macro approach
outline to the following:

1. Open n of N files
2. Select a subset based on a given criterion 3. Sort by some other
criterion 4. Compute a new variable based on two existing vars 5.
Aggregate into a new file

TIA

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Macro question

Marks, Jim
In reply to this post by Roberts, Michael
Jon:

I know 0 about python, but I am interested in learning-- how would I
accomplish this macro?

--jim



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Peck, Jon
Sent: Tuesday, July 11, 2006 2:54 PM
To: [hidden email]
Subject: Re: Macro question

This sounds straightforward if you have SPSS 14 and the programmability
plug-in -- no macro required but a little Python knowledge necessary.

-Jon Peck
SPSS

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Roberts, Michael
Sent: Tuesday, July 11, 2006 1:32 PM
To: [hidden email]
Subject: [SPSSX-L] Macro question

Hi list,

I do not have any macro experience and the spsstools macro tutorial
leaves me cold!  Would anyone be able to suggest a macro approach
outline to the following:

1. Open n of N files
2. Select a subset based on a given criterion 3. Sort by some other
criterion 4. Compute a new variable based on two existing vars 5.
Aggregate into a new file

TIA

Mike