DATA Step, Macro, Functions and more

How to use relative path to define files in SAS?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

How to use relative path to define files in SAS?

I'm trying to write a macro defining a file using FILENAME statement 

 

%let pathname= ~/NHAMCS/;
%macro assign(name =, file = );
	FILENAME name &pathname..&file;
%mend assign;

%assign(name = demo03, file = demo03.txt);

When I call the macro, it gave errors said physical files do not exist. Can anyone show me how to do it correct?


Accepted Solutions
Solution
‎01-12-2018 07:12 AM
Super User
Posts: 10,569

Re: How to use relative path to define files in SAS?

Posted in reply to Viveme789

BTW your macro should correctly use its parameters:

%macro assign(name=, file=);
filename &name "&pathname.&file"; /* only 1 dot needed */
%mend assign;

Since calling the macro will be almost the same amount of typing than just using the filename statement as is, the macro is unnecessary and will only obfuscate your code by adding a useless level of indirection that makes the code less maintainable.

Don't (ab)use the macro facility just because it is there, use it for a purpose.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Super User
Posts: 9,840

Re: How to use relative path to define files in SAS?

Posted in reply to Viveme789

What software are you using, and relative to what?  What is this supposed to be?

~/NHAMCS/

There are ways, but its different between the different versions of SAS (EG for instance is different to Base), and I would question why you need this in the first place.  If you want to store a file, you should know where you want to put it.  Even if its just a folder from a base point:

Occasional Contributor
Posts: 18

Re: How to use relative path to define files in SAS?

SAS university edition. I only need create a dataset but using FILENAME statement

Super User
Super User
Posts: 9,840

Re: How to use relative path to define files in SAS?

Posted in reply to Viveme789

You don't need filename statements to create datasets??  You create a library reference to a physical path, something like:

libname mydata '/folders/myfolders/';

I believe the /folders/ is the default area.  Then you simply use that library reference in your code to create a dataset:

data mydata.class;
  set sashelp.class;
run;

It may be a good idea to focus on the basics of the SAS environment and Base SAS programming, before jumping into Macro whic is an advanced topic and useful only in certain circumstances.

Super User
Posts: 10,569

Re: How to use relative path to define files in SAS?

[ Edited ]
Posted in reply to Viveme789

The tilde is often used to represent the user's home directory, eg in http URL's. But operating systems don't recognize that.

In UNIX, you should use the environment variable HOME, so your %let would look like this:

%let pathname= $HOME/NHAMCS/;

The nearest Windows equivalent would be %user%, which needs macro masking functions in SAS.

 

But since you use SAS UE (which runs on UNIX), you should keep all your files in /folders/myfolder, or subdirectories of that.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎01-12-2018 07:12 AM
Super User
Posts: 10,569

Re: How to use relative path to define files in SAS?

Posted in reply to Viveme789

BTW your macro should correctly use its parameters:

%macro assign(name=, file=);
filename &name "&pathname.&file"; /* only 1 dot needed */
%mend assign;

Since calling the macro will be almost the same amount of typing than just using the filename statement as is, the macro is unnecessary and will only obfuscate your code by adding a useless level of indirection that makes the code less maintainable.

Don't (ab)use the macro facility just because it is there, use it for a purpose.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 456 views
  • 0 likes
  • 3 in conversation