Roberto
Roberto

Reputation: 729

SAS - results of %let and libname combinations

I'm currently learning SAS and I got a code to be adapted and reused but first I have to understand it. My question is about just a small part of it (top of the code). Here it is:

%let dir=/home/user/PROJECT/CODES/;
%let dir_project=/home/user/PROJECT/;

libname inp "&dir_project" compress=yes;
libname out "&dir.out" compress=yes;

%let tg=out.vip;

My questions are:

  1. What does &dir.out mean? What is it referring to? I suppose it's something called "out". Is it looking for a database OUT? If yes and all my databases are usually temporary ones in WORK should I change it to WORK.OUT?
  2. What is the resulting path of "tg"? I doubt that it is: "/home/user/PROJECT/CODES/out.vip".

Originally the code was referring to some locations on C: drive but I work entirely in SAS Studio so I have to adapt it.

Thank you in advance

Upvotes: 1

Views: 3815

Answers (2)

Tom
Tom

Reputation: 51611

The first two statements define two macro variables, DIR and DIR_PROJECT. In the second two statements you use those macro variables to define two librefs, INP and OUT. The last statement just defines another macro variable named TG.

Macro variable references start with & and are followed by the name of the macro variable to expand. SAS will stop looking for the macro variable name when is sees a character that cannot be part of a macro variable name or a period. That is why the first libname statement uses the value of the DIR_PROJECT macro variable instead of the DIR macro variable. The period in the second libname statement tells SAS that you want to replace &dir. with the value of the macro variable DIR. If you had instead just written &dirout then SAS would look for a macro variable named DIROUT.

Macro variable just contain text. The meaning of the text depends on what SAS code you generate with them. So the first two macro variable look like they contain absolute paths to directories on your Unix file system, since they start from the root node / and end with a /. This is confirmed by how you use them to generate libname statements.

By adding the constant text out after the path in the second libname statement the result is that you are looking for a sub-directory named out in the directory that the value of the macro variable DIR names.

As for the last macro variable TG what it means depends on how it is used. Since it is of the form of two names separated by a period then it looks like it can be used to refer to a SAS dataset. Especially since the first name is the same as one of the librefs that you defined in the libname statements. So you might use that macro variable in code like this:

proc print data=&tg ; run;

Which would be expanded into:

proc print data=out.vip ; run;

In that case you are looking for the SAS dataset named VIP in the library named OUT. So you would be looking for the Unix file named:

/home/user/PROJECT/CODES/out/vip.sas7bdat

Now if you used that macro variable in some SQL code like this:

select &tg ...

Then it would expand to

select out.vip ....

and in that case you would be referencing a variable named VIP in an input dataset named (or aliased as) OUT.

Upvotes: 2

Allan Bowe
Allan Bowe

Reputation: 12691

1 - &dir. is a macro variable. The period marks the end of the variable, and thus &dir.out resolves to /home/user/PROJECT/CODES/out at runtime. Your libname statement will now link the libref out to this physical location.

2 - the tg variable is a dataset reference, in the form "library.dataset". Here, out is the library, and vip is the dataset. This way you can write code such as:

data &tg.;
  set sashelp.class; 
run;

To create the dataset vip in the out library.

In this way, you are in fact (almost) right. The resulting path of &tg. (which resolves to out.vip) will be /home/user/PROJECT/CODES/out/vip.sas7bdat.

Upvotes: 1

Related Questions