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!
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.
Ok.. Maybe I need to change the document name.
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;
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.
Yes, it can. It does. Show the program you used and the macro call, and we can adjust it if needed.
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;
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
@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)
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.
@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.
Even better would be to avoid weird data set names.
%macro mymacro(file);
data WORK.IMPORTED_&file ;
.... "/myaddress/&file..txt" ...
...
%mend;
%mymacro(1_apple)
,
Right...
I just rename all the documents as apple_1 and it can work on my original code.
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!
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.
Ready to level-up your skills? Choose your own adventure.