6

SAS libref is not recognized in the macro loop

 2 years ago
source link: https://www.codesd.com/item/sas-libref-is-not-recognized-in-the-macro-loop.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SAS libref is not recognized in the macro loop

advertisements

I've run into an odd SAS quirk that I can't figure out - hopefully you can help.

I have a simple macro loop that imports CSV files and for some reason if I use a libref statement in the "out=" part of the import procedure, SAS doesn't recognize the libref as a valid name. But if I use the same libref in a data step, it works just fine.

The specific error it gives is: "ERROR: "TESTDB." is not a valid name."

I'd like to figure this out because I work with pretty big files and want to avoid reading through them more times than is necessary.

Here's the code that works, with some comments in it. I got around the issue by reading in the files, then writing them to permanent SAS datasets in a second step, but ideally I'd like to import the files directly into the "TESTDB" library. Any idea how to get SAS to recognize a libref in the "out=" statement of the import procedure?

libname testdb "C:\SAS test";
%let filepath = C:\SAS test\;

%macro loop(values);
    %let count=%sysfunc(countw(&values));
    %do i = 1 %to &count;
        %let value = %qscan(&values,&i,%str(,));
        proc import datafile = "&filepath.&value..csv"
            out = &value dbms=csv replace; getnames=yes;
                     /*"out=testdb.&value" in the line above does not work*/
        run;
        data testdb.&value; set &value; run;
                 /*here the libref testdb works fine*/
    %end;
%mend;
%loop(%str(test_a,test_b,test_c));

Thanks in advance for your help!


Perhaps try:

out=testdb.%unquote(&value)

Sometimes the macro language does not unquote values automatically. With result that the extra quoting characters introduced by a quoting function (%qscan %str %bquote %superq etc) cause problems.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK