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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 832 views
  • 3 likes
  • 4 in conversation