DATA Step, Macro, Functions and more

Helpful macro to obtain current working directory with batch grid jobs on UNIX/Linux

Reply
Contributor
Posts: 31

Helpful macro to obtain current working directory with batch grid jobs on UNIX/Linux

[ Edited ]

Since you can no longer use "." to implicitly reference the current working directory for batch grid jobs, try this:

%global pwd cwd;

%macro wd;
filename envcmd pipe 'set | egrep "PWD=|LS_SUBCWD="' lrecl=1024;

data env;
  length var $9 val $200;
  infile envcmd dlm='=' missover;
  input var $ val $;

  if val ^= ''
  then do;
    if var = 'PWD'
    then do;
      pwd = val;
      put pwd=;
      call symput('pwd',strip(val));
    end;

    if var = 'LS_SUBCWD'
    then do;
      cwd = val;
      put cwd=;
      call symput('cwd',strip(val));
    end;
  end;
run;

%if %superq(cwd) ^=
%then %let pwd = &cwd.;

%put pwd=&pwd.;

libname pwd "&pwd.";
libname here "&pwd.";
%mend wd;

%wd;

* End of Macro Include *;

Example output:

(batch grid launch from command-line with sasgsub)
CWD=/path/where/sas/code/resides
PWD=/sas/config/compute/Lev1/SASApp

(batch grid launch from EG with XCMD and sasgsub)
CWD=/path/to/EG/home/where/sas/code/resides
PWD=/sas/config/compute/Lev1/SASApp


(interactive grid launch from EG)
CWD=/path/to/UNIX/home
PWD=/sas/config/compute/Lev1/SASApp


In the first two cases, we want the value of CWD to overwrite PWD and be captured in a macro-variable for assigning librefs, filerefs, etc. For the third case, it's not really helpful as neither path makes sense for assignment. This is really designed for capturing paths in batch grid jobs where the current working directory changes.

 

Community Manager
Posts: 3,462

Re: How to obtain working directory with grid-launched jobs

In what context do those last two lines work?  In another macro somewhere?  Or is your entire example meant to be enclosed in a macro?

Contributor
Posts: 31

Re: How to obtain working directory with grid-launched jobs

Posted in reply to ChrisHemedinger

@ChrisHemedinger, I updated my original post that hopefully answers your questions. I'd also be happy to discuss this more at GF.

Ask a Question
Discussion stats
  • 2 replies
  • 154 views
  • 1 like
  • 2 in conversation