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

Hi i have a problem inserting the data set into the macro command definition. I don't know how to call a specific numeric variable. Data set has 4 columns, 2 are text and the last 2 are numeric. I want to call a text variable to put it in a logical expression with% LENGHT.

This is the code. Below is the data.

SAS 9.4

 

561 %macro make(podaci,County);
562 %if %length(&County) gt 8 %then
563 %let County = %substr(&County,1,8);
564 filename &podaci "&County";
565 %mend make;
566 %make(prog.podaci18)
ERROR: Invalid logical name.
ERROR: Error in the FILENAME statement.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The parameters to a macro just populate local macro variables to the macro.  So in the code of the macro you use them the same as any other macro variable.  Say you had this code to run PROC FREQ.

 

proc freq data=prodaci18;
  tables country ;
run;

And you wanted to make a way to do the same thing for a different variable in the dataset.  First create a macro variable to hold the name of the variable and then replace the name of the variable with the reference to the macro variable.

%let varname=country;
proc freq data=prodaci18;
  tables &varname. ;
run;

Now you can convert this to a macro that has a parameter named varname.  Now you can pass in the name of the variable to use when generating the code when you call the macro.

%macro run_freq(varname);
proc freq data=prodaci18;
  tables &varname. ;
run;
%mend run_freq ;

%run_freq(varname=country);

Note that all three of these code blocks run the exact same SAS code. 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@malov0610 wrote:

I have a problem inserting the data set into the macro command definition.


This first sentence of yours does not really make sense to me. More information is needed. What does "insert" a data set mean? How do you want to insert this data set? Where do you want to insert this data set? What do these macro variables have to do with this data set? What should the result of the code be?

 

Please provide a lot more explanation. Please show us the code you have tried, even if it doesn't work.

--
Paige Miller
malov0610
Calcite | Level 5

I want a macro command that I will use on the data set I have attached. I want the macro command it would create to take values ​​from a Country variable that contains the names of the municipalities and put them in an expression next to the% LENGHT statement.

%macro make(podaci,County);
%if %length(&County) gt 8 %then
%let County = %substr(&County,1,8);
filename &podaci "&County";
%mend make;
%make(prog.podaci18)

Tom
Super User Tom
Super User

Macro commands operate on text and generate text.   Normally you use them to generate text that SAS can interpret as code.

 

So show the SAS code (not macro code) that you want to generate.  Explain what parts of that code needs to vary and explain how it should vary and logic needed. Then perhaps you can start to write a macro that can generate the code you need.

malov0610
Calcite | Level 5

okay, i'm curious when i create macros, the parameters in brackets are whether they refer to variables within the data set

Tom
Super User Tom
Super User

The parameters to a macro just populate local macro variables to the macro.  So in the code of the macro you use them the same as any other macro variable.  Say you had this code to run PROC FREQ.

 

proc freq data=prodaci18;
  tables country ;
run;

And you wanted to make a way to do the same thing for a different variable in the dataset.  First create a macro variable to hold the name of the variable and then replace the name of the variable with the reference to the macro variable.

%let varname=country;
proc freq data=prodaci18;
  tables &varname. ;
run;

Now you can convert this to a macro that has a parameter named varname.  Now you can pass in the name of the variable to use when generating the code when you call the macro.

%macro run_freq(varname);
proc freq data=prodaci18;
  tables &varname. ;
run;
%mend run_freq ;

%run_freq(varname=country);

Note that all three of these code blocks run the exact same SAS code. 

malov0610
Calcite | Level 5

Thank you very much for your help. I did it.

Shmuel
Garnet | Level 18

@malov0610 , it seems you are confused how to use macros.

First semantics: The next I use to call as MACRO PROGRAM which maybe you call as MACRO COMMAND:

%macro <macro_name> (...argumants...);
   ....
%mend <macro_name>;

Argument may be positional or named:

%macro <macro_name> (arg1 , arg2 , named1=... , named2= ...);

You defined 2 positional arguments in your code:

%macro make(podaci,County);

Then when you invoke the macro program/command you have to supply those two arguments.

writing %make(prog.podaci18) - miss ';' at the end of the statement, and what is  prog.podaci18 ? 

those are no the arguments you have to supply ! It seems more as a sas dataset name.

 

The code between %macro and %mend should contain the sas code that do what you want,

using macro variables, the arguments you supplied, for example:

%macro add(varname, vvalue);
data want;
   set have end=eof;
       output;
        if eof then do;   /* adding new obs */
           &varname = &vvalue;    /*for numeric variable , OR 
           &varname = "&vvalue" ;  << for char type variable */
run;
%mend;

%add(<new_ID>, <it value>);

           

 

malov0610
Calcite | Level 5


I am a little bit. I didn't understand where to define the data set. But thank you for your help

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 8 replies
  • 1679 views
  • 0 likes
  • 4 in conversation