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

I am having issues using text macros in the data steps.

It may be because the filenames/datasets have underscores (but not sure). Unfortunately filenames will have to have underscores in our system's setup.

 

Trial code 1:

%let text1 = xyz_abc_def;

%let text2 = abc;

 

-----this works just fine (when I am typing full name of text1 and using one macro for text2)

data xyz_abc_def_&text2;

set oldfile;

...

...

run

 

--but when I try to use two macros in the data step i get errors:

data &text1_&text2;

set oldfile;

...

...

run

Error: 

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, /, ;, _DATA_, _LAST_, _NULL_.

 

So then I try this:

 

data "&text1"_&text2;

set oldfile;

...

...

run

 

Error: 

ERROR: User does not have appropriate authorization level for library WC000001.

 

Please advice. Thanks in advance. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

First, let's get the terminology right. These are macro variables, not macros. Do not confuse the two.

 

To fix the problem, you need a dot after the name of the first macro variable to indicate that what follows the dot is considered text.

 

&text1._&text2

Otherwise SAS looks for a macro variable named &text1_ and there is no such macro variable defined.

 

 

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

First, let's get the terminology right. These are macro variables, not macros. Do not confuse the two.

 

To fix the problem, you need a dot after the name of the first macro variable to indicate that what follows the dot is considered text.

 

&text1._&text2

Otherwise SAS looks for a macro variable named &text1_ and there is no such macro variable defined.

 

 

--
Paige Miller
ubshams
Quartz | Level 8

On a somewhat related matter, I keep getting an error when trying to use a text macro in a proc import statement.

 

%let portfolio =portfolioname;

 

libname out "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio";

 

proc import out = out.samplefile_import datafile = "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio/filename_&portfolio._v2.xlsx"

Dbms=xlsx replace;

Sheet=EOP_Bal_Flow_Actuals;

 

Get following error:

WARNING: Missing semicolon between %THEN clause and ELSE has been assumed.

ERROR: Physical file does not exist, /folder1/folder2/folder3/folder4/folder5/folder6/portfolioname//Filename_portfolioname_v2.xlsx.

 

This occurs even though there is clearly an excel file called filename_portfolioname_v2.xlsx in the relevant folder. Also noticing two forward slashes in the error, not sure why.

 

Please advice. Thanks in Advance!

 

 

Tom
Super User Tom
Super User

@ubshams wrote:

On a somewhat related matter, I keep getting an error when trying to use a text macro in a proc import statement.

 

%let portfolio =portfolioname;

 

libname out "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio";

 

proc import out = out.samplefile_import datafile = "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio/filename_&portfolio._v2.xlsx"

Dbms=xlsx replace;

Sheet=EOP_Bal_Flow_Actuals;

 

Get following error:

WARNING: Missing semicolon between %THEN clause and ELSE has been assumed.

ERROR: Physical file does not exist, /folder1/folder2/folder3/folder4/folder5/folder6/portfolioname//Filename_portfolioname_v2.xlsx.

 

This occurs even though there is clearly an excel file called filename_portfolioname_v2.xlsx in the relevant folder. Also noticing two forward slashes in the error, not sure why.

 

Please advice. Thanks in Advance!

 

 


You have clearly posted the wrong part of the program. There is neither an %IF nor a %THEN keyword in any of the code you posted.

Why does the message seem to be a mix between a macro error and a data step error.  Why does it %THEN and ELSE instead of %THEN and %ELSE or THEN and ELSE?

ubshams
Quartz | Level 8

It might have been some global macros at the top of my code (some legacy companywide macros). Anyways I deleted those and just ran this part form the top and still get physicial file does not exist error.

 

%let portfolio =portfolioname;

 

libname out "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio";

 

proc import out = out.samplefile_import datafile = "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio/filename_&portfolio._v2.xlsx"

Dbms=xlsx replace;

Sheet=EOP_Bal_Flow_Actuals;

 

ERROR: Physical file does not exist, /folder1/folder2/folder3/folder4/folder5/folder6/portfolioname//Filename_portfolioname_v2.xlsx.

ScottBass
Rhodochrosite | Level 12

@ubshams wrote:

It might have been some global macros at the top of my code (some legacy companywide macros). Anyways I deleted those and just ran this part form the top and still get physicial file does not exist error.

 

%let portfolio =portfolioname;

 

libname out "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio";

 

proc import out = out.samplefile_import datafile = "/folder1/folder2/folder3/folder4/folder5/folder6/&portfolio/filename_&portfolio._v2.xlsx"

Dbms=xlsx replace;

Sheet=EOP_Bal_Flow_Actuals;

 

ERROR: Physical file does not exist, /folder1/folder2/folder3/folder4/folder5/folder6/portfolioname//Filename_portfolioname_v2.xlsx.


 

1) run your code with options symbolgen mprint mlogic (mlogic probably not needed but doesn't hurt), then cut-and-paste the relevant portion of your log (from the source code echoed in the log to the error message).  Paste it using the Insert SAS Code... icon.

 

2) For now forget macro.  Get this exact code to work properly (edit as required):

 

proc import 
   out = out.samplefile_import 
   datafile = "/folder1/folder2/folder3/folder4/folder5/folder6/portfolioname/filename_portfolioname_v2.xlsx"
   dbms=xlsx 
   replace;
   Sheet=EOP_Bal_Flow_Actuals;
run;

3) Once you get the exact code to work properly, since your only macro variables are in the datafile path, just do this:

 

%put /folder1/folder2/folder3/folder4/folder5/folder6/&portfolio/filename_&portfolio._v2.xlsx;

and just review the log until the %put statement exactly equals the code (filepath) that you proved worked as desired.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
ScottBass
Rhodochrosite | Level 12

P.S.:  Don't mark a post resolved, then post "On a somewhat related matter...".  Either unmark the resolved post or post a new thread.  Many members will just ignore posts that are resolved, so you're limiting your pool of potential solutions (and possibly lengthening the time to get a solution).


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
ubshams
Quartz | Level 8

The portfolioname macro in the context of filename is case sensitive it turns out. That fixed it. Thanks for all your suggestions!

 

ScottBass
Rhodochrosite | Level 12

@ubshams wrote:

The portfolioname macro in the context of filename is case sensitive it turns out. That fixed it. Thanks for all your suggestions!

 


 

Yeah unix/linux is like that :rolleyes:


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2995 views
  • 3 likes
  • 4 in conversation