BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mirisage
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
esjackso
Quartz | Level 8

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

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

EJ

View solution in original post

5 REPLIES 5
esjackso
Quartz | Level 8

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

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

EJ

Mirisage
Obsidian | Level 7

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

esjackso
Quartz | Level 8

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

Tom
Super User Tom
Super User

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.

Mirisage
Obsidian | Level 7

Hi esjackso1 and Tom,

I now understand.

Many thanks to both of you for this help.

Regards

Mirisage


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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 928 views
  • 1 like
  • 3 in conversation