DATA Step, Macro, Functions and more

Is there a way to retrieve the hostname?

Reply
N/A
Posts: 0

Is there a way to retrieve the hostname?

Hello everyone,
When testing one of our sas scripts on the development server we have to configure things slightly differently in the script (libnames pointing at different locations mainly), however it would be really useful if the script could automatically detect the hostname of the server it is currently running on. Does anyone know of a way to do this?
The help file plus google have not provided me with a solution yet.

Regards,
~Toby
N/A
Posts: 0

Re: Is there a way to retrieve the hostname?

HI,

I saw something about this yesterday actually. It seems v9.2 has a automatic macro variable called %syshostname

http://support.sas.com/documentation/cdl/en/whatsnew/62435/HTML/default/mcrolrefwhatsnew902.htm
Contributor
Posts: 22

Re: Is there a way to retrieve the hostname?

Use &syshostname instead of %syshostname

 

% is used to identify a macro, but & is used to show the value of a macro variable.

 

Needless to say, it's important to know the difference between a macro and a macro variable, but most people just call them macros.

N/A
Posts: 0

Re: Is there a way to retrieve the hostname?

Sadly I believe we are using 9.1 (tried %put &syshostname; but that returned an error).

~T
N/A
Posts: 0

Re: Is there a way to retrieve the hostname?

read the environment variables. One will be the computername or hostname. It is platform dependant. Once you discover which, you can retrieve it with
%let host= %SYSGET(envVarName);

PeterC
Super Contributor
Posts: 474

Re: Is there a way to retrieve the hostname?

Another way of doing that would be piping the result of hostname command:

data _null_;
infile 'hostname' pipe; /* pipe result of hostname comand */
hostname=_infile_; /* _infile_ is the input buffer */
put hostname=;
run;

this works on both UNIX and Windows systems.

Greetings from Portugal.

Daniel Santos at www.cgd.pt
Respected Advisor
Posts: 3,777

Re: Is there a way to retrieve the hostname?

You forgot one statement INPUT;
[pre]
data _null_;
infile 'hostname' pipe; /* pipe result of hostname comand */
input;
hostname=_infile_; /* _infile_ is the input buffer */
put hostname=;
run;
[/pre]
Super Contributor
Posts: 474

Re: Is there a way to retrieve the hostname?

Yep. True.

Thanks.

Greetings from Portugal.

Daniel Santos at www.cgd.pt
Respected Advisor
Posts: 3,890

Re: Is there a way to retrieve the hostname?

[ Edited ]

Whenever I can influence things, then I make sure that directory structures are identical between environments. I don't think coding environment smart within individual scripts is a good approach. 

 

What's sometimes different is the "Root path". What I'm normally doing in such a case is to implement SAS macro variables in the Autoexec and so I keep all environment specific information in a single location for maintenance.

 

A libname statement would then look like:

libname myds "&data\subfolder1\subfolder2";

 

Where the "subfolders" are the part of the directory structure under my control and identical in all environments.

 

An alternative approach for libraries used in multiple programs would be, to define these libraries in SAS Metadata. This is again about keeping environment specific things in a central location and not spread out over many programs which becomes a maintenance nightmare in case something ever changes - and for example the host name will change when you upgrade to a current SAS version in a hopefully not too far future for you.

 

If you don't have the necessary level of access to modify the Autoexec or to change SAS Metadata then a 3rd option would be to create a %init macro with all these environment specific definitions. Store the macro code in a folder which is part of the SAS Autocall facility and then just call the macro always at the beginning of your programs.

Super Contributor
Posts: 474

Re: Is there a way to retrieve the hostname?

Hi @Patrick

 

Hmmm, somehow I think you posted in the wrong topic.

 

This is a 7 years old discussion Smiley Happy

 

Daniel Santos @ www.cgd.pt

Respected Advisor
Posts: 3,890

Re: Is there a way to retrieve the hostname?

@DanielSantos

LOL! How did that happen? No idea how I've even got to such an old thread.

Ask a Question
Discussion stats
  • 10 replies
  • 1524 views
  • 1 like
  • 5 in conversation