Can strings be inserted into file paths?

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

Can strings be inserted into file paths?

Ron0z
I can’t seem to find a way of manipulating a file path.  Perhaps it’s just not possible.  I have the following:

GET DATA /TYPE=XLSX
  /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\BP160211.xlsx'
  /SHEET=name 'BP'
  /CELLRANGE=full
  /READNAMES=on
  /ASSUMEDSTRWIDTH=32767.
EXECUTE.

which was predefined for me in the list file when I did File > Open > Data and then selected a spreadsheet I wanted to open.  I just copied and pasted the code.

Now that I’ve got the code I wanted to add something like the following:

String p1 (A8).
Compute p1 = ‘BP160211’.

and change the path in the above to
  /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\p1.xlsx'

SPSS didn't like it.  (File doesn't exist or is being used.)  So, I tried to set it up as a macro (with my new found knowledge) by making p1 a parameter (I remembered my ! when I did so), but still no go.
Reply | Threaded
Open this post in threaded view
|

Re: Can strings be inserted into file paths?

Bruce Weaver
Administrator
The FILE HANDLE command may be what you want.  

http://www-01.ibm.com/support/knowledgecenter/SSLVMB_23.0.0/spss/base/syn_file_handle_examples.dita

Alternatively, you could indeed use macros.  The following is untested, but should give you the idea.  (I'm using forward slashes to make the code closer to usable on Macs.  Macs demand forward slashes; Windows, on the other hand, is very forgiving on this issue, and automatically converts them to backward slashes.)

DEFINE !Path () "I:/PLANNING/POLICY/STATS/2016/CIT/BBStats/" !ENDDEFINE.
DEFINE !p1 () "BP160211" !ENDDEFINE.
DEFINE !ext () ".xlsx" ENDDEFINE.

GET DATA /TYPE=XLSX
  /FILE= !Path + !p1 + !ext
  /SHEET=name 'BP'
  /CELLRANGE=full
  /READNAMES=on
  /ASSUMEDSTRWIDTH=32767.


Another option would be to enclose the GET DATA command in a macro, and pass the path, file name and extension to it as arguments.  If you provide a bit of context, it might be clearer what the best solution is for you.

HTH.


Ron0z wrote
I can’t seem to find a way of manipulating a file path.  Perhaps it’s just not possible.  I have the following:

GET DATA /TYPE=XLSX
  /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\BP160211.xlsx'
  /SHEET=name 'BP'
  /CELLRANGE=full
  /READNAMES=on
  /ASSUMEDSTRWIDTH=32767.
EXECUTE.

which was predefined for me in the list file when I did File > Open > Data and then selected a spreadsheet I wanted to open.  I just copied and pasted the code.

Now that I’ve got the code I wanted to add something like the following:

String p1 (A8).
Compute p1 = ‘BP160211’.

and change the path in the above to
  /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\p1.xlsx'

SPSS didn't like it.  (File doesn't exist or is being used.)  So, I tried to set it up as a macro (with my new found knowledge) by making p1 a parameter (I remembered my ! when I did so), but still no go.
--
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: Can strings be inserted into file paths?

Jon Peck
Using literal concatenation as Bruce has done will work, because the + here causes the literals on both sides of it to be joined so that they function as if they had been typed together,  but to clarify why the OP approach did not, note that the COMPUTE command is creating a casewise variable (p1) while the syntax would need a single value that can be evaluated by the syntax parser without any reference to the data.

On Mon, Feb 15, 2016 at 6:58 PM, Bruce Weaver <[hidden email]> wrote:
The FILE HANDLE command may be what you want.

http://www-01.ibm.com/support/knowledgecenter/SSLVMB_23.0.0/spss/base/syn_file_handle_examples.dita

Alternatively, you could indeed use macros.  The following is untested, but
should give you the idea.  (I'm using forward slashes to make the code
closer to usable on Macs.  Macs demand forward slashes; Windows, on the
other hand, is very forgiving on this issue, and automatically converts them
to backward slashes.)

DEFINE !Path () "I:/PLANNING/POLICY/STATS/2016/CIT/BBStats/" !ENDDEFINE.
DEFINE !p1 () "BP160211" !ENDDEFINE.
DEFINE !ext () ".xlsx" ENDDEFINE.

GET DATA /TYPE=XLSX
  /FILE= !Path + !p1 + !ext
  /SHEET=name 'BP'
  /CELLRANGE=full
  /READNAMES=on
  /ASSUMEDSTRWIDTH=32767.


Another option would be to enclose the GET DATA command in a macro, and pass
the path, file name and extension to it as arguments.  If you provide a bit
of context, it might be clearer what the best solution is for you.

HTH.



Ron0z wrote
> I can’t seem to find a way of manipulating a file path.  Perhaps it’s just
> not possible.  I have the following:
>
> GET DATA /TYPE=XLSX
>   /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\BP160211.xlsx'
>   /SHEET=name 'BP'
>   /CELLRANGE=full
>   /READNAMES=on
>   /ASSUMEDSTRWIDTH=32767.
> EXECUTE.
>
> which was predefined for me in the list file when I did File > Open > Data
> and then selected a spreadsheet I wanted to open.  I just copied and
> pasted the code.
>
> Now that I’ve got the code I wanted to add something like the following:
>
> String p1 (A8).
> Compute p1 = ‘BP160211’.
>
> and change the path in the above to
>   /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\p1.xlsx'
>
> SPSS didn't like it.  (File doesn't exist or is being used.)  So, I tried
> to set it up as a macro (with my new found knowledge) by making p1 a
> parameter (I remembered my ! when I did so), but still no go.





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

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Can-strings-be-inserted-into-file-paths-tp5731528p5731530.html
Sent from the SPSSX Discussion mailing list archive at 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
Reply | Threaded
Open this post in threaded view
|

Re: Can strings be inserted into file paths?

David Marso
Administrator
In reply to this post by Ron0z
What Jon and Bruce said...
"So, I tried to set it up as a macro (with my new found knowledge) by making p1 a parameter (I remembered my ! when I did so), but still no go. "

Any particular reason why you neglected to post your MACRO attempt?
Maybe there is a blatant/obvious reason for the "no go".
---
Ron0z wrote
I can’t seem to find a way of manipulating a file path.  Perhaps it’s just not possible.  I have the following:

GET DATA /TYPE=XLSX
  /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\BP160211.xlsx'
  /SHEET=name 'BP'
  /CELLRANGE=full
  /READNAMES=on
  /ASSUMEDSTRWIDTH=32767.
EXECUTE.

which was predefined for me in the list file when I did File > Open > Data and then selected a spreadsheet I wanted to open.  I just copied and pasted the code.

Now that I’ve got the code I wanted to add something like the following:

String p1 (A8).
Compute p1 = ‘BP160211’.

and change the path in the above to
  /FILE='I:\PLANNING\POLICY\STATS\2016\CIT\BBStats\p1.xlsx'

SPSS didn't like it.  (File doesn't exist or is being used.)  So, I tried to set it up as a macro (with my new found knowledge) by making p1 a parameter (I remembered my ! when I did so), but still no go.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"