BookmarkSubscribeRSS Feed
Panianka
Calcite | Level 5

Dear all,

 

I am stuck with rather simple task: I have several databases named country_year stored in respective directories by country and I have to open them to extract some data.

So I have a macro scrolling the list of countries, creating a library and treating the data base I need.

 

Here is the code 

 

%macro buyerslist(list, year, out);

 

**********************************************************************

First I scroll the list and at every step get a country from the list

**********************************************************************

 

%let n=%sysfunc(countw(&list.));

%do i=1 %to &n;

%let bu=%qscan(&list.,&i.,%str( ));

 

*********************************************************************************************

I creat the library for respective country and call a data base  named country_year

***********************************************************************************************

 

libname BUdata "DIRECTORY\&bu.\";

%put Budata.&bu._&year.;

 

data &out..&bu._buyers (keep = buyers );

set BUdata.&bu._&year.;

run;

 

*******************************************************

This part somehow does not work.

 

%put Budata.&bu._&year.;  gives a correct result (EX &bu=ES , &year=2014)

 

Budata.ES_2014

 

But set BUdata.&bu._&year.  doesn't work correctly, it doesn't read "ES_2014" as one but cuts it in two peaces "Budata.ES"  and "_2014"  

 

ERROR: File BUDATA.ES.DATA does not exist.

ERROR: File WORK._2014.DATA does not exist.

 

 

Ofcause set BUdata.&bu_&year. wouldn't work since SAS will read it as a non existing macro variable

 

1 BUdata.&bu_2014

-------

22

201

WARNING: Apparent symbolic reference BU_2014 not resolved.

 

What should I do to call a data base in a macro with a name composed of two macro variables?

 

*******************************************************

 

 

run;

 

libname BUdata clear;

 

 

%end;

%mend buyerslist;

 

 

 

 

%let bulist =

ES

NL

FR

DE

UK

IT

IE

CZ;

 

%let exers_year=2016;

%let Prev_year=2014;

 

 

 

libname misc "\DIR\misc\";

%buyerslist(&bulist., misc, &Prev_year.);

4 REPLIES 4
mkeintz
PROC Star

Does this fix it?

 

    %let dsn=%trim(&bu)_&year;

    set BUdata.&dsn;

 

MK

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star

You're right. The program isn't behaving properly.  Experiments to try ...

 

First, try retyping the BU list:

 

%let bulist = ES NL FR DE UK IT IE CZ;

 

It's conceivable that the editor used to construct the original %LET statement introduced characters that SAS isn't parsing correctly (carriage returns, line feeds).

 

If that doesn't work, try applying %UNQUOTE:

 

set %unquote(BUdata.&bu._&year.);

 

That may overcome the way that SAS is treating the pieces as separate strings.

Panianka
Calcite | Level 5

Thanks a lot, with uniqote it works for me.

Shmuel
Garnet | Level 18

Try use:

 

set %sysfunc(compress(BUdata.&bu._&year.));

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 1110 views
  • 3 likes
  • 4 in conversation