Include macros in SAS

This macro will include the external macros in a library on the disk. This will be done, so you are sure that SAS will choose the external macros not the same macros in libraries included in SASAUTOS. The macros first choosen by SAS will be the macros found in Work.Sasmacr library. Including the external macros will put these macros in this library. Example : %IncludeExternalMacros(dir=&RootPath/Macros/External/);

%macro IncludeExternalMacros(dir=);
%local fileref rc did dnum dmem memname filename didc;                                                                                
%let rc=%sysfunc(filename(fileref,&dir));                                                                                             
%let did=%sysfunc(dopen(&fileref));                                                                                                   
%if &did=0 %then %do;                                                                                                                 
%put ERROR: Directory does not exist;                                                                                               
%put ERROR: The macro will terminate;                                                                                             
%return;                                                                                                                            
%end;                                                                                                                                 
%let dnum=%sysfunc(dnum(&did));                                                                                                       
%do dmem=1 %to &dnum;                                                                                                                 
%let memname=%qsysfunc(dread(&did,&dmem));                                                                                          
%if %qupcase(%qscan(&memname,-1,.)) = SAS %then %do;                                                                              
%let filename=%scan(&memname,1,.);                                                                                              
%inc "&dir.&filename..sas";                                                                                                     
%end;                                                                                                                             
%end;                                                                                                                                 
%let didc=%sysfunc(dclose(&did));                                                                                                     
%let rc=%sysfunc(filename(fileref));                                                                                                  
%mend;

It’s also possible to use this simple piece of SAS-code found below.

filename Macros "<Path>";

%inc Macros("*.sas");

 

 

Get execution information from SAS

The code below makes a print to your log containing different information related to your current SAS-session.
The code is heavily inspired from this article.

/* The macro need to know if your running the program on a remote server. The default is that the program is running on a remote server. If that is the case, the macro gets some information from the remote server. */ %macro GetExecInfo(rsubmit=yes);

/* Removes the %put-statement below. Makes it possible to get a fine print in your log. */ 
options nosource;

/* The programname and path. */
%let SASProgram = %sysget(sas_execfilepath);

/* If your running SAS on a remote server your need to supply the remote SAS with information about the local program being executed.*/ 
%if %upcase(&RSubmit) = YES %then 
%do; 
%syslput SASProgram = &SASProgram; 
%end;

%if %upcase(&RSubmit) = YES %then 
%do; 
rsubmit; 
%end;

%macro GetInfo; /* AIX (UNIX) systems and probably other systems doesn't have the same environment variables as eg Windows servers. */ 
%if &sysscpl = AIX %then 
%do; 
%let ServerName = N/A; 
/* It is possible to try the statements below to get the servername from an AIX-server. */ 
/*    host = sysget('HOST'); 
- OR - filename h pipe 'hostname'; 
data _null_; 
infile h; 
input; 
put _infile_; 
run; 
*/ 
%end; 
%else 
%do; 
%let ServerName = %sysget(computername); 
%end;

/* Inserts blank lines in the log. */ 
skip; 
skip; 
%put ******************************************************************;
%put *      Program : &SASProgram (Revision: ); 
%put *      Date : &sysday, &sysdate9; 
%put *      User : &sysuserid; 
%put *      Server : &ServerName; 
%put *      OS version : &sysscp (&sysscpl); 
%put *      SAS version : &sysvlong4; 
%put ******************************************************************; skip; 
skip; 
%mend; 

%GetInfo;

%if %upcase(&RSubmit) = YES %then 
%do; 
endrsubmit; 
%end;

options source; 
%mend;

%GetExecInfo(rsubmit=yes);

 

 

Options in SASv9.cfg

The options below can be used to congfigure SAS through the file SASv9.cfg
The file is usually located in c:\program files\SAS\SASFoundation\9.2\nls\en\.
NB! Depending on your version of SAS.
You can find the installation directory for SASv9.cfg through the SAS GUI. Go to Tools -> System Options.

SASConfig

Option Description
-WORK w:\saswork\!username If the username for the users using SAS is eg APM this options creates a Work directory under w:\saswork\ called APM – w:\saswork\APM\. This makes it easier for the users to retrieve the tables in the work-directory and easier for system administration to locate users that takes up a lot of storage on the work-drive.
-odsdest=listing If you don’t like the default HTML-output. This option sets the output destination to listing. The output from SAS will now be shown like it used to.
 -odsgraphics=off If you don’t like the default HTML-output. This option will turn off ODS Graphics. The output from SAS will now be shown like it used to.
-MEMSIZE 4G Sets the maximum aout of RAM/memory that can be used during a SAS-session. In this case 4GB.
-SORTSIZE 2G Sets the maximum amount of RAM/memory that SORT procedure can use during a SAS-session. In this case 2GB.

Getting accessible schemas for a user

The code below gets the schemas from a Microsoft SQL-server that is accessible for a given user. It is made with SAS-code but the SQL-statement can be used regardless of using SAS.

proc sql noprint;
connect to odbc(datasrc=&_datasrc user=Your user; pwd="Your password");
select schema_name into :_schema_names separated by " "
from connection to odbc
(
select s.name as schema_name
from sys.schemas as s
inner join
sys.database_permissions as dbp
on s.schema_id=dbp.major_id
inner join
sys.sysusers as u
on
u.uid=dbp.Grantee_principal_id
where dbp.Class_desc='SCHEMA' and lower(u.name) = %str(%')Your domain\&uid.%str(%')
);

%put &_schema_names;
disconnect from odbc;
quit;

Loop through datasets in SAS

This code was found here. It lets you loop through datasets in a SAS-library and tries to print the first 10 observations from each dataset in the SAS-library.

proc contents data=<Your library>._all_ noprint out=temp (keep=memname);
run;

proc sort data=temp nodupkey;
     by memname;
run;

data _null_;
	set temp;
	call execute ('proc print data='|| memname || '(obs=10); title "*** ' || trim(memname) || ' ***"; run;');
run;

 

Call Execute lets you run PROC-statements in a datastep. It will simply execute the proc-statement after finishing the datastep. It is also possible to use ‘if‘, ‘then‘ and ‘else‘ in the datastep to execute certain parts of the code based on some value.
This is of course also possible through macro-code.

Writing a SAS-program in SAS

The code below will let you write a SAS-program in SAS.

/* This dummy macro-variable will be used later as input for the SAS program. */
%let HelloWorld = Hello world;
data _null_;
file SASProgram;
put ‘%let Hello='” &HelloWorld. “‘;’;
put ‘%put &Hello;’;
put ‘run;’;
run;

filename cdrive “C:\SASPrograms”;

data _null_;
infile SASProgram;
file cdrive(HelloWorld.sas);
input;
put _infile_;
run;
filename cdrive clear;

You now have a SAS-program called ‘HelloWorld.sas‘ in the folder ‘C:\SASPrograms\‘.
The program will look like this.

%let Hello= Hello World;
%put &Hello;
run;

And it will write ‘Hello World‘ to the log in SAS.

Signon to remote SAS

Below is an example of how you can signon to a remote SAS-server and execute SAS-commands.

/* Insert the name of the server that you want to connect to and the number of the port. It is usually 7551. The portnumber is the where your local SAS for the SAS/CONNECT service on the remote machine. */
%let remote=<Your server> 7551;
/* Gets your userid from the system and prompts you for your password when connecting. */
options comamid=tcp remote=remote; signon remote user=&sysuserid passwd=_prompt_;

/* Makes a libname for your workdirectory on the remote server. */
libname remtwork slibref=work server=remote;

/* The macro below is executed on the remote server and creates a libname for a userfolder on the remote server. The userfolder hos to have the same name as the macro &sysuserid. */
rsubmit;
%macro UserLib;
%let user = %sysfunc(substr(&sysuserid,4));
libname &user.Lib “f:\users\&sysuserid”;
options compress=binary;
%mend;
%UserLib;
endrsubmit;

/* This creates a local libname that points to the user libname on the remote server. */
%let user = %sysfunc(substr(&sysuserid,4));
libname &user.Lib slibref=&user.Lib server=remote;

SAS and SVN

Below is shown how you can extract the current SVN revision number for a given path into SAS and use it in your SAS program.

/* The path for your&nbsp;SAS-program(s) that is versioned in SVN.&nbsp;*/
%let sysin=F:\MySASPrograms\TestProgram;
/* The path for the SVN program SubWCRev.exe. This is the default path, your path could be different. */
%let SubWCRev=C:\Program Files\TortoiseSVN\bin\SubWCRev.exe;
%put &sysin;

filename svnver pipe "call ""&SubWCRev"" ""&sysin"" ";

data _null_;
infile svnver;
input;
/* The output is in three different lines. */
put _n_ _infile_;
/* Puts the three different output lines in three diffent macrovariables called svnver1, svnver2 and svnver3. */
call symput('svnver' !! put(_n_,1.), _infile_);
run;

You can now use the three diffent macrovariables called svnver1, svnver2 and svnver3 in your program.

You might also want to have a look at this blogpost regarding TFS.

CrystalDiskMark and Windows Azure

On my Windows Azure trial account I created a large virtual server with Windows Server 2008R2 64-bit and testet it with CrystalDiskMark 3.0.2.

As showen below, the result was quite amazing.

AzureDisk

 

 

 

 

 

 

 

 

 

I was later told that storage for a customer on Windows Azure at the first i placed on SSD-disks. Later on the storage is moved away from SSD-disks and placed on a ordinary magnetic spindel disks according to your required workload.