DATA Step, Macro, Functions and more

How to use macros to apply macros from text file on csv dataset

Reply
Occasional Contributor
Posts: 6

How to use macros to apply macros from text file on csv dataset

[ Edited ]
 

I have a text file "Macro definition" which has two SAS macros definition. I would like to import them and apply on HTWT.csv data set. This dataset has 20 observations and 6 variables ID, Gender, Age,Height,Weight,Year. All are numeric except gender variable. I have the code below to import and apply the macros from the txt file to csv file. I am getting an error message on running this code as below.

 

%macro import_myfile(i=);
proc import file="C:\Users\komal\Desktop\Advanced SAS\Macro definition.txt" out=Macrodefinition dlm='09x' replace;
run;
%mend import_myfile;
%include Macrodefinition;

Error: Unable to complete processing of INCLUDE. Expected a filename or fileref
Failed to open macro : The system cannot find the file specified

 

Please let me know your advice on how to solve it. Thank you for your time.
Super User
Posts: 9,573

Re: How do I use macros to apply macros from text file on csv dataset


@Komal2 wrote:
 

I have a text file "Macro definition" which has two SAS macros definition. I would like to import them and apply on HTWT.csv data set. This dataset has 20 observations and 6 variables ID, Gender, Age,Height,Weight,Year. All are numeric except gender variable. I have the code below to import and apply the macros from the txt file to csv file. I am getting an error message on running this code as below.

 

%macro import_myfile(i=);
proc import file="C:\Users\komal\Desktop\Advanced SAS\Macro definition.txt" out=Macrodefinition dlm='09x' replace;
run;
%mend import_myfile;
%include Macrodefinition;

Error: Unable to complete processing of INCLUDE. Expected a filename or fileref
Failed to open macro : The system cannot find the file specified

 

Please let me know your advice on how to solve it. Thank you for your time.

Your macro definition for import_myfile is completely unnecessary. There's no dynamic code at all, parameter &i is never used. Stop making things unnecessarily complicated.

 

And a %include statement needs either a physical filename, or a file reference created by a filename statement.

Either add a statement

filename Macrodefinition "physical filename comes here";

or use the physical filename in quotes in the %include.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,540

Re: How do I use macros to apply macros from text file on csv dataset

Text files don't need to be imported.  They just need to exist.  You might use:

 

%include "C:\Users\komal\Desktop\Advanced SAS\Macro definition.txt";

 

Then any macros defined within that text file will be available.

Occasional Contributor
Posts: 6

Re: How do I use macros to apply macros from text file on csv dataset

Posted in reply to Astounding
Thanks. How can I apply the 2 macros from the Txt file to HTWT. csv
dataset?


Super User
Posts: 6,540

Re: How do I use macros to apply macros from text file on csv dataset

That's something you need to learn.  There are multiple ways, depending on how the macros are defined.  You will need to roll up your sleeves, find some documentation and study it.  This might be a decent starting point:

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0pfmkjlc3e719n1lks4go8ke61r.htm&docset...

Occasional Contributor
Posts: 6

Re: How do I use macros to apply macros from text file on csv dataset

[ Edited ]
Posted in reply to Astounding
%include "C:\Users\komal\Desktop\Advanced SAS\Macro definition.txt";

I have tried to use the code above to call the macro.

 

I am getting an error message below. I have checked the location of the file and it is correct.

 

 

Unable to compllete processing of INCLUDE. Expected a filename or fileref.

 

 

Is there something I am missing here?

Super User
Posts: 6,540

Re: How do I use macros to apply macros from text file on csv dataset

Just to set the record straight, this statement will not call the macro.  It will make the macro available within your program, so that later statements within the program can call the macro.

 

Let's see the log that contains the error.  That's the usual source of debugging information.

Super User
Posts: 9,573

Re: How do I use macros to apply macros from text file on csv dataset

Please use copy and paste for logs, don't type them off the screen.

 

Unable to compllete processing of INCLUDE. Expected a filename or fileref. 

SAS logs are correctly spelled, so it would be "complete" instead of "compllete".

Copy/paste the whole log resulting from your include into a {i} window, no changes, no additions, no omissions.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 6

Re: How do I use macros to apply macros from text file on csv dataset

Posted in reply to KurtBremser

I am working on SAS in remote desktop. I am unable to copy/paste from SAS in remote desktop.

Occasional Contributor
Posts: 6

Re: How do I use macros to apply macros from text file on csv dataset

Posted in reply to Astounding

I tried the code below to call the macro.

outcsvv is the name of HTWT dataset imported in SAS.

%include "C:\Users\komal\Desktop\Advanced SAS\Macro definition.txt";
%contents_of(outcsvv)
%print_data(outcsvv)

Error: Unable to complete processing of INCLUDE. Expected a filename or fileref
Expected a statement keyword: found "("

The second error I am getting is probably due to the macro definition(s) from the text file which are as follows.

%macro contents_of(name);
proc contents data=&name;
run;
%mend;


%macro print_data(name);
proc print data=&name;
run;
%mend;

Please let me know your advice on how to solve it. Thanks

Super User
Posts: 6,540

Re: How do I use macros to apply macros from text file on csv dataset

There are communication issues that I don't understand when you are working from a remote desktop.  How do you refer to a file from the desktop, when the file is stored elsewhere?  For a single-machine environment, it looks like you are taking all the right steps (including calling the macros).

 

Certainly for these two macros you don't need macros.  It's easy enough to code:

 

proc contents data=outcsvv;

run;

proc print data=outcsvv;

run;

 

So can I assume that these are smaller versions of what the macros actually contain?

 

 

Super User
Posts: 9,573

Re: How do I use macros to apply macros from text file on csv dataset

I would not be surprised at all if there's some translation issue of keystrokes between the remote and local desktop, as SAS seems to misinterpret the whole filename string (including the quotes)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 22,846

Re: How to use macros to apply macros from text file on csv dataset

Can you share your macro definitions?

I would try with a .sas file that you know is correct to see if it's a path issue or a file issue. 

Ask a Question
Discussion stats
  • 12 replies
  • 202 views
  • 1 like
  • 4 in conversation