Hello, in the program below, I generate a macro variable called "K" that corresponds to the number of categories of a certain variable. I find myself writing this sort of statement repetitively so I tried to form it into a macro program. The program executes fine and correctly assigns a value to "K" but %K is not usable outside of the mend statement. How do I save %K to use in future steps?
%MACRO MAKE_DUM (VAR); PROC SQL; CREATE TABLE COUNT_DIAG AS SELECT DISTINCT &VAR. FROM DRUGS; QUIT; DATA COUNT_DIAG; SET COUNT_DIAG; ID=_N_; RUN; PROC SQL; SELECT MAX(ID) INTO :NOBS FROM COUNT_DIAG;
QUIT; %LET K=%EVAL(&NOBS); %MEND MAKE_DUM;
%MAKE_DUM(DIAG_2);
1. Make your variable global (default is local which means it only exists in the macro)
2. Simplify your macro by using the automatic macro variable SQLOBS which stores the number of records saved in the table in this case.
%MACRO MAKE_DUM (VAR);
PROC SQL;
CREATE TABLE COUNT_DIAG AS
SELECT DISTINCT &VAR.
FROM DRUGS;
QUIT;
%Global K;
%let k = &sqlobs;
%MEND MAKE_DUM;
%MAKE_DUM(DIAG_2);
%put &k.;
@dandy_jim wrote:
Hello, in the program below, I generate a macro variable called "K" that corresponds to the number of categories of a certain variable. I find myself writing this sort of statement repetitively so I tried to form it into a macro program. The program executes fine and correctly assigns a value to "K" but %K is not usable outside of the mend statement. How do I save %K to use in future steps?
%MACRO MAKE_DUM (VAR); PROC SQL; CREATE TABLE COUNT_DIAG AS SELECT DISTINCT &VAR. FROM DRUGS; QUIT; DATA COUNT_DIAG; SET COUNT_DIAG; ID=_N_; RUN; PROC SQL; SELECT MAX(ID) INTO :NOBS FROM COUNT_DIAG;
QUIT; %LET K=%EVAL(&NOBS); %MEND MAKE_DUM;
%MAKE_DUM(DIAG_2);
1. Make your variable global (default is local which means it only exists in the macro)
2. Simplify your macro by using the automatic macro variable SQLOBS which stores the number of records saved in the table in this case.
%MACRO MAKE_DUM (VAR);
PROC SQL;
CREATE TABLE COUNT_DIAG AS
SELECT DISTINCT &VAR.
FROM DRUGS;
QUIT;
%Global K;
%let k = &sqlobs;
%MEND MAKE_DUM;
%MAKE_DUM(DIAG_2);
%put &k.;
@dandy_jim wrote:
Hello, in the program below, I generate a macro variable called "K" that corresponds to the number of categories of a certain variable. I find myself writing this sort of statement repetitively so I tried to form it into a macro program. The program executes fine and correctly assigns a value to "K" but %K is not usable outside of the mend statement. How do I save %K to use in future steps?
%MACRO MAKE_DUM (VAR); PROC SQL; CREATE TABLE COUNT_DIAG AS SELECT DISTINCT &VAR. FROM DRUGS; QUIT; DATA COUNT_DIAG; SET COUNT_DIAG; ID=_N_; RUN; PROC SQL; SELECT MAX(ID) INTO :NOBS FROM COUNT_DIAG;
QUIT; %LET K=%EVAL(&NOBS); %MEND MAKE_DUM;
%MAKE_DUM(DIAG_2);
By default macro variables created when a macro is running are defined as LOCAL to that macro's scope. So they disappear when the macro has ended its execution.
You can either create the macro variable before calling the macro so that it already exists.
%let K=before macro call;
%MAKE_DUM(DIAG_2);
%put &=k ;
Or make your macro smart enough to define it as a GLOBAL instead of LOCAL.
You should probably make the macro smart enough not to make it GLOBAL if it already exists are LOCAL to some other macro that is calling this one. Especially for something that looks like a utility macro you might want to call in your other larger macros.
%MACRO MAKE_DUM (VAR);
%if not %symexist(K) %then %global k ;
PROC SQL;
CREATE TABLE COUNT_DIAG AS
SELECT DISTINCT &VAR.
FROM DRUGS;
QUIT;
%let k=&sqlobs;
%MEND MAKE_DUM;
%MAKE_DUM(DIAG_2);
Assuming that you don't need COUNT_DIAG for any other purpose, just simplify the program. If you simplify it enough, running the program would be just as easy as calling a macro. For example:
proc sql;
select count(distinct(diag_2)) into : k from drugs;
quit;
With no macro defined, you won't have any difficulty using &K, and the program will likely be easier to understand.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.