BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Viveme789
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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:

Viveme789
Fluorite | Level 6

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User

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.

 

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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