How to assign a library to the same path as your SAS Enterprise Guide project

9

SAS Enterprise Guide sets values for several useful SAS macro variables when it connects to a SAS session, including one macro variable, &_CLIENTPROJECTPATH, that contains the name and path of the current SAS Enterprise Guide project file.

(To learn about this and other macro variables that SAS Enterprise Guide assigns, look in Help->SAS Enterprise Guide help, and search the Index for "macro variables".)

If your SAS program knows the path of your project file, then you can use that information to make your project more "portable", and reference data resources using paths that are relative to the project location, instead of having to hard-code absolute paths into your program.

For example, I've been working with some data from DonorsChoose.org, and I've captured that work in a project file. After saving the project, I can easily get the name of the project file by checking the value of &_CLIENTPROJECTPATH:

15    %put &_clientprojectpath;
'C:\DataSources\DonorsChoose\DonorsChoose.egp'

If you can get the full path of the project file, then you can build that information into a SAS program so that as you move the project file and its "assets" (such as data), you don't need to make changes to the project to accommodate a new project folder "home". Here is the bit of code that takes the project file location and distills a path from it, and then uses it to assign a path-based SAS library.
/* a bit of code to detect the local project path                          */
/* NOTE: &_CLIENTPROJECTPATH is set only if you saved the project already! */
%let localProjectPath =
%sysfunc(substr(%sysfunc(dequote(&_CLIENTPROJECTPATH)), 1,
%sysfunc(findc(%sysfunc(dequote(&_CLIENTPROJECTPATH)), %str(/), -255 )))); 

libname DC "&localProjectPath";

Here's the output:
21  libname DC "&localProjectPath";
NOTE: Libref DC was successfully assigned as follows:
Engine:        V9
Physical Name: C:\DataSources\DonorsChoose

This allows me to keep the project together with the data that it consumes and creates. And I can easily share the project and data with a colleague, who can store it in a different folder on his/her machine, and it should work just the same without any changes.

Share

About Author

Chris Hemedinger

Senior Manager, SAS Online Communities

+Chris Hemedinger is the manager of SAS Online Communities. He’s also co-author of the popular SAS for Dummies book, author of Custom Tasks for SAS Enterprise Guide using Microsoft .NET, and a frequent participant on the SAS Enterprise Guide discussion forum.

Related Posts

9 Comments

  1. Andrew Howell on

    Hi Chris,

    This works fine if EG & SAS are on the same PC (or at least map the same way to the same folder).

    What are suggestions when EG & the underlying SAS are on different platforms and/or don't map to same folder?

    For example:
    - EG on the PC connecting to SAS on Unix
    - EG project on a PC's mapped "Z:" drive connecting to SAS on a WIndows server where the equivalent folder is "D:"?

    Cheers.

  2. Tricia Aanderud on

    Yeah. I guess the problem is that the .egp is a PC format and it just will not translate to Unix in the desired way.

    But awesome tip Chris!!

  3. Actually, it can work IF you save the project to the server file system. For example, File->Save As, choose SASApp (your server), Files, and navigate to a file folder on the remote SAS server.

    In that case _CLIENTPROJECTPATH is set to the path and name on the remote server, even if it's Unix. For example:

    23 %put &_clientprojectpath;
    '/r/server.unx.sas.com/vol/vol910/u91/sascrh/projects/SampleCars.egp'

  4. Pingback: Special automatic macro variables available in SAS Enterprise Guide - The SAS Dummy

  5. What do I do wrong? Part of the log:
    15 %let localProjectPath =
    16 %sysfunc(substr(%sysfunc(dequote(&_CLIENTPROJECTPATH)), 1,
    WARNING: Argument 3 to function SUBSTR referenced by the %SYSFUNC or %QSYSFUNC macro function
    is out of range.
    17 %sysfunc(findc(%sysfunc(dequote(&_CLIENTPROJECTPATH)), %str(/), -25 ))));
    18
    19 libname DC "&localProjectPath";
    NOTE: Libref DC was successfully assigned as follows:
    Engine: V9
    Physical Name: C:\Windows\system32\

  6. Now I ran it with SASApp but still the wrong path:

    WARNING: Argument 3 to function SUBSTR referenced by the %SYSFUNC or %QSYSFUNC macro function is out
    of range.
    17 %sysfunc(findc(%sysfunc(dequote(&_CLIENTPROJECTPATH)), %str(/), -255 ))));
    18
    19 libname DC "&localProjectPath";
    NOTE: Libref DC was successfully assigned as follows:
    Engine: V9
    Physical Name: E:\SAS\Config\Lev1\SASApp\

    • Chris Hemedinger
      Chris Hemedinger on

      The CLIENTPROJECTPATH macro yields the folder name for the EGP file on your local machine. That means if your SAS workspace is remote, then you won't be able to use that path in your programs this way.

      See this list of other macro variables that might be helpful for your SAS programs.

      • Hi, I had the same problem as Hend. For me, the solution was to change '%str(/)' in '%str(\)'.

        This solved the warning and pointed localProjecPath to the right location.

        With '%str(/)' same problem

        NOTE: Libref DC was successfully assigned as follows: Engine: V9 Physical Name: C:\Windows\system32\

        With '%str($$' problem solved
        NOTE: Libref DC was successfully assigned as follows: Engine: V9 Physical Name: \\NAS\Test

        With this problem solved it works great! So many thanks Chris!!

        Another solution I found was to use environment variables

        Code:
        options set=localProjectPath '\\NAS\Test' ; libname DC "!localProjectPath";
        Result:

        NOTE: Libref DC was successfully assigned as follows: Engine: V9 Physical Name: \\NAS\Test

        • I have just come to the same conclusion myself. I was just about to post a comment about it. Perhaps I should have also read the comments before spending time trying to figure out why it wasn't working for me.

Leave A Reply

Back to Top