How to list your mapped drives in a SAS program

6

If you work in a team environment, you might be accustomed to using mapped network drives for source data folders or to publish results. If you've recently moved to a SAS server environment, you might not have those mapped drives available. How can you tell?

This question was posted on the SAS Support Communities, and SAS Communities member Patrick provided a handy code snippet that does the trick. Patrick learned the trick himself from SAS Note 24818.

The following code uses a special DRIVEMAP device for the FILENAME statement. DRIVEMAP works only on Windows systems, which are the only systems that support the concept of mapped drives.

filename diskinfo DRIVEMAP;
data _null_;
  infile diskinfo;
  input;
  put _infile_;
run;

Here's the output on my PC, where I have a two mapped network drives, U: and W:. (C: and D: are part of my laptop device.)

NOTE: The infile DISKINFO is:
      Drivemap Access Device,
      PROCESS,RECFM=V,LRECL=32767
C:
D:
U:
W:

Mapped drives are typically available only from your local machine. Even if you use SAS on a remote Windows server, the process that defines the mapped drive aliases is usually not triggered when you connect. If this is the case for you, you'll have to adjust your process to use UNC path notation (example: "\\server\folder\project" instead of the familiar "P:\project" shortcut). (Additional note for SAS admins: on a Windows SAS server, you must mark the server machine as Trusted for Delegation to allow end users to access these network paths.)

If you've moved from a local PC version of SAS to a remote non-Windows server (these days, Linux is common), then I'm afraid that "mapped drives" are a lost concept. You'll need to change your code to use Unix-style paths. A SAS admin or Unix-admin might need to help you to find the correct locations.

Share

About Author

Chris Hemedinger

Director, SAS User Engagement

+Chris Hemedinger is the Director of SAS User Engagement, which includes our SAS Communities and SAS User Groups. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies

6 Comments

  1. That's nifty, Chris. Too bad it doesn't show the UNC for the mapped drives. Assume this works even without XCMD turned on? For those who do have XCMD, an alternative which does give the UNC is: filename diskinfo pipe "net use" ;

    I thought it might be handy to write a %GetUNC function-style macro, it turns out Art Carpenter has already written one: http://www2.sas.com/proceedings/forum2008/023-2008.pdf. : )

    • Chris Hemedinger
      Chris Hemedinger on

      "The Path, the Whole Path, and Nothing But the Path, So Help Me Windows" -- a great title. And a nice throwback to the CALL MODULE and SASCBTBL methods, which I documented as part of the Windows SAS companion after Rick Langston released the methods in SAS V6.11 (1995).

  2. Hi Chris,

    I had a similar task to solve some time ago, I also used PIPE (same as Quentin) but instead "net ues" I used windows's wmic command: "wmic logicaldisk get name, description" which gave me not only drive letter but also its description.

    Code like that:
    filename diskinfo PIPE "wmic logicaldisk get name, description";
    data _null_;
    infile diskinfo;
    input;
    if index(_INFILE_,":") then put _infile_;
    run;

    returned:
    Local Fixed Disk C:
    Local Fixed Disk D:
    Removable Disk G:

    • Chris Hemedinger
      Chris Hemedinger on

      Good tip! That works great locally when you have XCMD privileges. DRIVEMAP is less detailed, but doesn't need to XCMD privileges (I think...). In practice it won't make so much of a difference, as mapped drives are most relevant for users of local SAS. When connecting to a remote session (where XCMD might be disabled), mapped drives are probably not used.

  3. We've got a Linux install with a few 'bi-' drives (accessible both to Windows and Linux), so I put this macro together :

    %macro conv2url(fqfile);
    	%put &=fqfile;
    	%let cpp = &fqfile;
    	%let cpp2 = %sysfunc(dequote(&cpp));
    	%put &=cpp &=cpp2;
     
    	/* determine current path and file */
    	%let posit = %sysfunc(FINDC(%qleft(%superq(fqfile)), %str(\), b) );
    	%Let conv2urlcp = %Substr(%qleft(%superq(fqfile)),1,&posit );
    	%Put 0 &=conv2urlcp;
    	%let posit = %sysfunc(FINDC(%qleft(%superq(fqfile)), %str(\), b) );
    	%Let filename = %Substr(%qleft(%superq(fqfile)),&posit + 1,%length(&fqfile)-&posit );
    	%Put 1 &=conv2urlcp &=filename;
    	%let driveletter = %Substr(%qleft(%superq(conv2urlcp)),1,1);
    	%put &=driveletter;
    	%let url = &&drive&driveletter.%str(\);
    	%put &=url;
    	%let drive = %Substr(%qleft(%superq(conv2urlcp)),1,3);
    	%Let conv2urlcp = %SysFunc( TranWrd( %qleft(%superq(conv2urlcp)) ,&drive, &url ) );
    	%LET conv2urlcp= %SysFunc( TranWrd( %qleft(%superq(conv2urlcp)),%str(\),%str(/)));
    	%Put 2 &=conv2urlcp;
    	%put &=filename;
    	%let urlfile = &conv2urlcp.&filename;
    	%put &=urlfile;
    	&conv2urlcp.&filename;
    %mend conv2url;

    So with this global variable:
    %let DRIVES = /mnt/nfs/prod/Teams/statutory/REGREP;

    I can do this:
    %let urlfile=%conv2url(S:\_Louisiana\173Report\Vendor_Files\Transportation\Report-1017.xlsx);
    %put &=urlfile;

    And get this:
    URLFILE=/mnt/nfs/prod/Teams/statutory/REGREP/_Louisiana/173Report/Vendor_Files/Transportation/Report-1017.xlsx

    And then I can do a proc import on that file, for example.

    Or someone can enter the S: path and filename and the macro will convert it for them.

Back to Top