FW: Re: [SPSSX-L] Breaking up string variables

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

FW: Re: [SPSSX-L] Breaking up string variables

Melissa Ives
Sent this earlier but didn't see it come through--I see that Hal 9000 had the same idea...
Or...if the last 6 characters are always the ones to drop (2 digit state and 2 digit country codes plus 2 hyphens) You could do something like
        compute city=substr(location,1,length(location)-6))

Then it never matters how many hyphens there are within the city name.

Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Melissa Ives
Sent: Thursday, July 19, 2007 1:10 PM
To: [hidden email]
Subject: Re: [SPSSX-L] Breaking up string variables

It may be better to work from the right, rather than from the left to find the characters prior to the 2nd to last hyphen.

Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Peck, Jon
Sent: Thursday, July 19, 2007 12:41 PM
To: [hidden email]
Subject: Re: [SPSSX-L] Breaking up string variables

Well, what about
aix-en-provence-pa-us
?

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Marta Garcia-Granero
Sent: Thursday, July 19, 2007 3:46 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Breaking up string variables

Marks, Jim escribió:
> Watch out for "Wilkes-Barre-PA-US"
>
>
Thanks for pointing it out. How about this then?:

* Sample dataset*.
DATA LIST LIST/location(A20).
BEGIN DATA
"Ithica-NY-US."
"Wilkes-Barre-PA-US"
END DATA.

STRING #step city (A20).
* In two steps *.
COMPUTE #step = SUBSTR(location,1,RINDEX(location,"-")-1).
COMPUTE city = SUBSTR(#step,1,RINDEX(#step,"-")-1).
LIST.


> --jim
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf
> Of Marta Garcia-Granero
> Sent: Wednesday, July 18, 2007 2:41 AM
> To: [hidden email]
> Subject: Re: Breaking up string variables
>
> Hi Matthew:
>
> Try this:
>
> * Sample dataset*.
> DATA LIST LIST/location(A20).
> BEGIN DATA
> "Ithica-NY-US."
> END DATA.
>
> STRING city (A20) .
> COMPUTE city=SUBSTR(location,1,INDEX(location,"-")-1).
> LIST.
>
> HTH,
> Marta
>
>>   Easy question. I have a string variable, we'll call "location", in
>>
> the form of "Ithica-NY-US." I need to extract the city component of
> this variable to create a string "city" variable, so, for present
> purposes, all I would really be concerned with is Ithica.
>
>>   I tried the following (below), which uses RTRIM, figuring that I
>>
> could just conveniently "trim away" from the right. First step was to
> compute citystat (e.g., Ithica-NY), thinking that the RTRIM function
> may only trim off from the first hyphen on the right. Since I wanted
> to get only city, I would then have to compute the city variable by
> doing the RTRIM function again, but on citystat  But it doesn't quite
> do the job (actually, it doesn't do much of anything), so I'm likely
> either using RTRIM incorrectly or I need to use a different function.
> Seems like a pretty routine procedure, so I took a quick browse
> through the user's guide, but couldn't find much. What is the easiest way to do this?
>
>>   STRING citystat (A20) .
>>   COMPUTE citystat=RTRIM(location, "-").
>>   execute .
>>
>>   STRING city (A20) .
>>   COMPUTE city=RTRIM(citystat, "-").
>>   execute .
>>
>>
>>
>>
>
>


PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND CONFIDENTIAL information and is intended only for the use of the addressee. If you are not the designated recipient, or an employee or agent authorized to deliver such transmittals to the designated recipient, you are hereby notified that any dissemination, copying or publication of this transmittal is strictly prohibited. If you have received this transmittal in error, please notify us immediately by replying to the sender and delete this copy from your system. You may also call us at (309) 827-6026 for assistance.
Reply | Threaded
Open this post in threaded view
|

Macro Variable within Macro Question

Jeff-125
I'm not great with spss macros, so I may have some terminology incorrect here.

I'm defining a macro variable (or perhaps I should just call it a
macro) with the first 3 lines of code below to create a path to where
data files are stored.
The purpose is to be able to use a long and complex syntax file on
more than one machine when the data files are stored in different
places by only altering the path on these 3 lines and not the 50 or
so places that the syntax file will use that path. The example get
file command works fine using this technique.

The MyCode1 macro example does not work when the !Path macro variable
is used/called within the MyCode1 macro as indicated below.

I think that the question boils down to whether I can call one macro
from within another macro, and if so how?  ...or how might I do what
I'm attempting to accomplish in another fashion?

Thanks

Jeff


define !Path ()
  'F:\Data\'
!enddefine.


** This Works **.
get file = !Path+"1992DataFile.sav".

** this macro does not work with a similar command line **.
Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
!DO !var=!start !TO !finish.
    Get file=!Quote(!Concat(!Path,!var,"DataFile.sav")).    <- this
does not work
    Get file=!Quote(!Concat( 'F:\Data\',!var,"DataFile.sav")).  <-
this does work.

<do a bunch of stuff>

!DoEnd.
!EndDefine.

!MyCode1 start=1992 finish=1993.
exe.
Reply | Threaded
Open this post in threaded view
|

Re: Macro Variable within Macro Question

Catherine Kubitschek
Jeff,

To get your macro to work I had to surround the !Path with a !unquote and a
!eval.  I also used the set mprint on command to help with the
debugging.  This works for me:

set mprint on .
define !Path ()
  'D:\SPSS\Temp\tmp_t' !enddefine.

Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
!DO !var=!start !TO !finish.
    Get file=!Quote(!Concat(!unquote(!eval(!Path)),!var,".sav")).
!DoEnd.
!EndDefine.

!MyCode1 start=1 finish=2.
exe .

I changed a couple of strings so I could test this with my data but the
important changes for you are around the !Path.

You can definitely call a macro from within another macro but sometimes it
is tricky.

Catherine

At 7/19/2007 05:20 PM, Jeff wrote:

>I'm not great with spss macros, so I may have some terminology incorrect here.
>
>I'm defining a macro variable (or perhaps I should just call it a
>macro) with the first 3 lines of code below to create a path to where
>data files are stored.
>The purpose is to be able to use a long and complex syntax file on
>more than one machine when the data files are stored in different
>places by only altering the path on these 3 lines and not the 50 or
>so places that the syntax file will use that path. The example get
>file command works fine using this technique.
>
>The MyCode1 macro example does not work when the !Path macro variable
>is used/called within the MyCode1 macro as indicated below.
>
>I think that the question boils down to whether I can call one macro
>from within another macro, and if so how?  ...or how might I do what
>I'm attempting to accomplish in another fashion?
>
>Thanks
>
>Jeff
>
>
>define !Path ()
>  'F:\Data\'
>!enddefine.
>
>
>** This Works **.
>get file = !Path+"1992DataFile.sav".
>
>** this macro does not work with a similar command line **.
>Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
>!DO !var=!start !TO !finish.
>    Get file=!Quote(!Concat(!Path,!var,"DataFile.sav")).    <- this
>does not work
>    Get file=!Quote(!Concat( 'F:\Data\',!var,"DataFile.sav")).  <-
>this does work.
>
><do a bunch of stuff>
>
>!DoEnd.
>!EndDefine.
>
>!MyCode1 start=1992 finish=1993.
>exe.
Reply | Threaded
Open this post in threaded view
|

Re: Macro Variable within Macro Question

Peck, Jon
Note that it is not necessary to use a macro to abstract a file reference from a syntax stream.  A simpler way that is also clearer to someone reading the syntax is to define a FILE HANDLE and then reference that name.  File handles can specify directories or entire file paths.

You can also use the CD command to set the backend working directory and then use relative paths.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Catherine Kubitschek
Sent: Thursday, July 19, 2007 5:16 PM
To: [hidden email]
Subject: Re: [SPSSX-L] Macro Variable within Macro Question

Jeff,

To get your macro to work I had to surround the !Path with a !unquote and a
!eval.  I also used the set mprint on command to help with the
debugging.  This works for me:

set mprint on .
define !Path ()
  'D:\SPSS\Temp\tmp_t' !enddefine.

Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
!DO !var=!start !TO !finish.
    Get file=!Quote(!Concat(!unquote(!eval(!Path)),!var,".sav")).
!DoEnd.
!EndDefine.

!MyCode1 start=1 finish=2.
exe .

I changed a couple of strings so I could test this with my data but the
important changes for you are around the !Path.

You can definitely call a macro from within another macro but sometimes it
is tricky.

Catherine

At 7/19/2007 05:20 PM, Jeff wrote:

>I'm not great with spss macros, so I may have some terminology incorrect here.
>
>I'm defining a macro variable (or perhaps I should just call it a
>macro) with the first 3 lines of code below to create a path to where
>data files are stored.
>The purpose is to be able to use a long and complex syntax file on
>more than one machine when the data files are stored in different
>places by only altering the path on these 3 lines and not the 50 or
>so places that the syntax file will use that path. The example get
>file command works fine using this technique.
>
>The MyCode1 macro example does not work when the !Path macro variable
>is used/called within the MyCode1 macro as indicated below.
>
>I think that the question boils down to whether I can call one macro
>from within another macro, and if so how?  ...or how might I do what
>I'm attempting to accomplish in another fashion?
>
>Thanks
>
>Jeff
>
>
>define !Path ()
>  'F:\Data\'
>!enddefine.
>
>
>** This Works **.
>get file = !Path+"1992DataFile.sav".
>
>** this macro does not work with a similar command line **.
>Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
>!DO !var=!start !TO !finish.
>    Get file=!Quote(!Concat(!Path,!var,"DataFile.sav")).    <- this
>does not work
>    Get file=!Quote(!Concat( 'F:\Data\',!var,"DataFile.sav")).  <-
>this does work.
>
><do a bunch of stuff>
>
>!DoEnd.
>!EndDefine.
>
>!MyCode1 start=1992 finish=1993.
>exe.
Reply | Threaded
Open this post in threaded view
|

AW: Re: Macro Variable within Macro Question

Georg Maubach
Hi All,

FILE HANDLE can be used for reading and writing files if you have SPSS 15. It is not possible to write files with SPSS 12 using FILE HANDLE. We discovered a data corruption when doing writing to a file using FILE HANDLE with SPSS 12. It took us several days to find out what the reason was. But if you have the actual version of SPSS everything is fine.

Best regards

Georg Maubach


-----Ursprüngliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von Peck, Jon
Gesendet: Freitag, 20. Juli 2007 14:15
An: [hidden email]
Betreff: Re: Macro Variable within Macro Question

Note that it is not necessary to use a macro to abstract a file reference from a syntax stream.  A simpler way that is also clearer to someone reading the syntax is to define a FILE HANDLE and then reference that name.  File handles can specify directories or entire file paths.

You can also use the CD command to set the backend working directory and then use relative paths.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Catherine Kubitschek
Sent: Thursday, July 19, 2007 5:16 PM
To: [hidden email]
Subject: Re: [SPSSX-L] Macro Variable within Macro Question

Jeff,

To get your macro to work I had to surround the !Path with a !unquote and a !eval.  I also used the set mprint on command to help with the debugging.  This works for me:

set mprint on .
define !Path ()
  'D:\SPSS\Temp\tmp_t' !enddefine.

Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
!DO !var=!start !TO !finish.
    Get file=!Quote(!Concat(!unquote(!eval(!Path)),!var,".sav")).
!DoEnd.
!EndDefine.

!MyCode1 start=1 finish=2.
exe .

I changed a couple of strings so I could test this with my data but the important changes for you are around the !Path.

You can definitely call a macro from within another macro but sometimes it is tricky.

Catherine

At 7/19/2007 05:20 PM, Jeff wrote:

>I'm not great with spss macros, so I may have some terminology incorrect here.
>
>I'm defining a macro variable (or perhaps I should just call it a
>macro) with the first 3 lines of code below to create a path to where
>data files are stored.
>The purpose is to be able to use a long and complex syntax file on more
>than one machine when the data files are stored in different places by
>only altering the path on these 3 lines and not the 50 or so places
>that the syntax file will use that path. The example get file command
>works fine using this technique.
>
>The MyCode1 macro example does not work when the !Path macro variable
>is used/called within the MyCode1 macro as indicated below.
>
>I think that the question boils down to whether I can call one macro
>from within another macro, and if so how?  ...or how might I do what
>I'm attempting to accomplish in another fashion?
>
>Thanks
>
>Jeff
>
>
>define !Path ()
>  'F:\Data\'
>!enddefine.
>
>
>** This Works **.
>get file = !Path+"1992DataFile.sav".
>
>** this macro does not work with a similar command line **.
>Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
>!DO !var=!start !TO !finish.
>    Get file=!Quote(!Concat(!Path,!var,"DataFile.sav")).    <- this
>does not work
>    Get file=!Quote(!Concat( 'F:\Data\',!var,"DataFile.sav")).  <- this
>does work.
>
><do a bunch of stuff>
>
>!DoEnd.
>!EndDefine.
>
>!MyCode1 start=1992 finish=1993.
>exe.
Reply | Threaded
Open this post in threaded view
|

Re: Macro Variable within Macro Question

Jeff-125
In reply to this post by Jeff-125
At 10:05 PM 7/19/2007, you wrote:


That did it. Thanks





>To get your macro to work I had to surround the !Path with a
>!unquote and a !eval.  I also used the set mprint on command to help
>with the debugging.  This works for me:
>
>set mprint on .
>define !Path ()
>  'D:\SPSS\Temp\tmp_t' !enddefine.
>
>Define !MyCode1 (start=!Tokens (1) /finish=!Tokens (1)).
>!DO !var=!start !TO !finish.
>    Get file=!Quote(!Concat(!unquote(!eval(!Path)),!var,".sav")).
>!DoEnd.
>!EndDefine.
>
>!MyCode1 start=1 finish=2.
>exe .
>
>I changed a couple of strings so I could test this with my data but
>the important changes for you are around the !Path.
>
>You can definitely call a macro from within another macro but
>sometimes it is tricky.
>
>Catherine