BookmarkSubscribeRSS Feed
jordenlam
Calcite | Level 5

Hi All,

 

I try to read multiple txt documents into SAS with number at beginning of documents name, like 1_apple, 2_pear, 3_peach... It can work if I import it one by one(code 1), but fails when in the macro(code 2). 

 

 

 

code 1

data '1_Apple'n;
infile "/myaddress/1_Apple.txt" dsd dlm = ',';
input Number :$17.
Country :$18.;
run;

 

data in.'1_Apple'n;
set '1_Apple'n;
run;

 

 

 

code 2

%macro import(filename);
data &filename;
infile "/myaddress/&filename..txt" dsd dlm = ',';
input Number :$17.
Country :$18.;
run;

 

data in.&filename;
set &filename;
run;

%mend import;

 

%import(Apple_1);----------------------(It can work but the number is put at the end of document name)
%import(1_Apple);----------------------(ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.) 
%import('1_Apple'n);-------------------(ERROR: Physical file does not exist, /myaddress/'1_Apple'n.txt.)

 

 

I wonder is there any method that I can name it with the number at the beginning?

 

Many thanks!

21 REPLIES 21
andreas_lds
Jade | Level 19

As far as i know, macro variables must start with either letter or underscore, fortunately the ugly name literals don't exist in the macro-language.

jordenlam
Calcite | Level 5

Ok.. Maybe I need to change the document name.

Astounding
PROC Star

With slight changes to the macro, you should be able to get this version to work:

 

%import(1_Apple)

 

Change the last DATA step within the macro so it uses double quotes:

 

data in."&filename"n;
set "&filename"n;
run;
jordenlam
Calcite | Level 5

Thanks! But the result is still the same. (apple_1 can work but not 1_apple)

 

I believe the problem should be over there:

infile "/myaddress/&filename..txt" dsd dlm = ',';

 

&filename. cannot refer to 1_apple correctly.

Astounding
PROC Star

Yes, it can.  It does.  Show the program you used and the macro call, and we can adjust it if needed.

Tom
Super User Tom
Super User

Didn't you notice the difference between your first code

data '1_Apple'n;

and the code the macro is generating?

data 1_Apple;

You just need to make the macro generate a name literal like your original code did.

data "&filename"n;
jordenlam
Calcite | Level 5

Thanks! I tried. But it will stop because there is no physical file in /myaddress/'1_apple'n.txt. It should be /myaddress/1_apple.txt

Tom
Super User Tom
Super User

@jordenlam wrote:

Thanks! I tried. But it will stop because there is no physical file in /myaddress/'1_apple'n.txt. It should be /myaddress/1_apple.txt


Why did you change the way you were building the physical filename? There shouldn't have been any trouble with that.

%macro mymacro(file);

data "&file"n ;
   .... "/myaddress/&file..txt" ...
...
%mend;
%mymacro(1_apple)
jordenlam
Calcite | Level 5

Oh, that's because if I use 1_apple directly, the number "1" will be treated as a separate part with "_apple". You can see that the number "1" has turn out to be blue instead of black font. 

Tom
Super User Tom
Super User

@jordenlam wrote:

Oh, that's because if I use 1_apple directly, the number "1" will be treated as a separate part with "_apple". You can see that the number "1" has turn out to be blue instead of black font. 


I am pretty sure that the SAS compiler is color blind.

 

The colors you see in your editor is just the editor trying to help you catch obvious typos or syntax errors.  But the editor is NOT the SAS compiler so it will produce many strange colors on perfectly fine code.

ChrisNZ
Tourmaline | Level 20

Even better would be to avoid weird data set names.

 

%macro mymacro(file);

data WORK.IMPORTED_&file ;
   .... "/myaddress/&file..txt" ...
...
%mend;
%mymacro(1_apple)

,

Reeza
Super User
I think perhaps you should change your requirements just slightly. The import file name is 1_apple but perhaps name your SAS datasets 1_apple? You can name them 1_apple but then you run into issues, so I highly recommend changing the name, which is easy enough. Are all your files the same format/type? Are they all in the same folder? If so, you can read them in a single data step. If this makes sense, please reply back and someone can help you with that code.
jordenlam
Calcite | Level 5

I just rename all the documents as apple_1 and it can work on my original code.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 21 replies
  • 3278 views
  • 4 likes
  • 7 in conversation