Help using Base SAS procedures

Can we macronize the libnames?

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

Can we macronize the libnames?

Hi SAS Forum,

I ran the following two macros for two products, i.e. mango and orange.

MACRO I:


%macro anyname(mth=, month=, product=);
title "Distribution of values of Income variable in &product file in &month";

proc freq data=mango.&product._&mth; /*Why can't I change libname mango to &product*/
  tables county;
    run;
%mend;

%anyname(mth=Dec12, month=Dec2012, product=mango);
%anyname(mth=Jan13, month=Jan2013, product=mango);
%anyname(mth=Feb13, month=Feb2013, product=mango);
%anyname(mth=Mar13, month=Mar2013, product=mango);


MACRO 2:

%macro anyname(mth=, month=, product=);
title "Distribution of values of Income variable in &product file in &month";

proc freq data=orange.&product._&mth; /*Why can't I change orange to &orange*/

   tables village ;
    run;
%mend;

%anyname(mth=Dec12, month=Dec2012, product=orange);
%anyname(mth=Jan13, month=Jan2013, product=orange);
%anyname(mth=Feb13, month=Feb2013, product=orange);
%anyname(mth=Mar13, month=Mar2013, product=orange);

Background for Question;
As Mango and Orange data are found in two different libraries, namely "Mango" and "Orange",
it necessitated two macros, MACRO I and 2.

I tried to macronize libnames too to avoid MACRO 2, but it doesn't seem working.

Question:

Can't we macronize libnames (as shown in red and pink colors above).

Thank you for your help.

Mirisage


Accepted Solutions
Solution
‎04-18-2013 03:15 PM
Super Contributor
Posts: 334

Re: Can we macronize the libnames?

Did you have a double period after the libname macro var?

data= &libname..&product.__&mth ;

EJ

View solution in original post


All Replies
Solution
‎04-18-2013 03:15 PM
Super Contributor
Posts: 334

Re: Can we macronize the libnames?

Did you have a double period after the libname macro var?

data= &libname..&product.__&mth ;

EJ

Super Contributor
Posts: 338

Re: Can we macronize the libnames?

Hi esjackso1,

Thank you very much, your suggestion worked well.

Could you pls let me know why we need to include a double period?

I can only understand the places where we need to include a single period though.

Thank you for your time.

Mirisage

Super Contributor
Posts: 334

Re: Can we macronize the libnames?

You need it in two places that come to mind: file names and libname refereces both because they have period within the syntax, if memory serves e because the period and acts as a delimiter like a space when the macro is complied:

&test..xls --> where &test. is the macro variable and .xls is the file extension syntax --> without the extra period the file name fails because improper filename (ie no extension).

&libname..data --> where &libname. is the macro variable and .data is the dataset name syntax --> without it probably reads as the name of a temporary dataset.

Hope that helps!

EJ

Super User
Super User
Posts: 7,076

Re: Can we macronize the libnames?

SAS macro language needs to know how to figure out what macro variable you are trying to reference.  In most situation you can rely on the presence of spaces and other characters that are invalid to use as part of a variable name.  But to handle the situations where you want to reference a macro variable and append characters and digits the language allows you to follow the macro variable name with a period.  For example if I had a macro variable, VARNAME, with the base name of a variable and I wanted to use it to generate code to reference a new variable with _POST appended to it.  If I reference &VARNAME_POST the macro processor (and a human reading the code) would look for a macro variable name VARNAME_POST.  If you add the period then it is clear that macro variable name is VARNAME and that _POST is just constant text.  &VARNAME._POST.

So in your situation where you want generate a string (dataset reference) that includes a period you need to add an extra period.  The first one will be used by the macro processor to signal the end of the macro variable reference and the second one will become the constant text to use as part of the dataset reference.

Super Contributor
Posts: 338

Re: Can we macronize the libnames?

Hi esjackso1 and Tom,

I now understand.

Many thanks to both of you for this help.

Regards

Mirisage


🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 279 views
  • 1 like
  • 3 in conversation