am new to sas marco. I try to define new variable SOURCE_DT
and SOURCE_TIM
within macro but fail.
Could someone guide me on this? Please pardon me if the errors below are too silly.😳
`timvar `will be the var(A) in `indata``` that looks like HH:MM. I want to create var source_tim=A.
%macro m_test(indata, outdata, dtvar=., timvar=.);
data &outdata.;
set &indata.(encoding=any);
SOURCE_TIM=.;
%if (&timvar. ne .) %then %do;
SOURCE_TIM= &timvar. ;
%END;
SOUCE_DT=&dtvar.;
keep SOURCE_TIM SOURCE_DT;
RUN;
%mend m_test;
Show an actual example dataset that is the input.
Show the SAS code you want to generate (forget about make a macro or using macro variables for now).
Show the dataset you want to get from the input.
Explain what parts of the code needs to change for other datasets.
For example you might use SASHELP.CLASS as the input data and create a new variable named AGE2 that is the square of the value of age.
data want;
set sashelp.class;
age2 = age ** 2;
run;
Now if you wanted to make a macro that could could generate AGE2 using different formulas you could first create a macro variable, call it say FORMULA.
%let formula=age ** 2;
And then change the data step to:
data want;
set sashelp.class;
age2 = &formula ;
run;
You can now convert that to a macro with FORMULA as one of its parameters.
%macro mymacro(formula);
data want;
set sashelp.class;
age2 = &formula ;
run;
%mend mymacro;
Which you could then call like this to generate the same data step we started with.
%mymacro(formula=age ** 2)
But you could also pass something else. For example if you wanted AGE2 to be 21 for everyone you could just use:
%mymacro(formula=21)
Or perhaps you really want AGE2 to be a character variable.
%mymacro(formula="Age of Student")
Or perhaps
%mymacro(formula=put(age,Z3.))
So what data step do you want to run? Once you know that you can begin to try and make a macro.
For example for your macro if you wanted SOURCE_TIM to be set to 11 AM you might call it like this:
%m_test(indata=sashelp.class, outdata=want, dtvar=., timvar='11:00't);
So that it generates the statement:
SOURCE_TIM='11:00't ;
Why are you dropping all of the other variables?
We can remove that 'keep' line and keep all variables. the problem came up for the 2 variables that i wanted to create using marco variables. I want the real variable, not the variable name. 😳
Show an actual example dataset that is the input.
Show the SAS code you want to generate (forget about make a macro or using macro variables for now).
Show the dataset you want to get from the input.
Explain what parts of the code needs to change for other datasets.
For example you might use SASHELP.CLASS as the input data and create a new variable named AGE2 that is the square of the value of age.
data want;
set sashelp.class;
age2 = age ** 2;
run;
Now if you wanted to make a macro that could could generate AGE2 using different formulas you could first create a macro variable, call it say FORMULA.
%let formula=age ** 2;
And then change the data step to:
data want;
set sashelp.class;
age2 = &formula ;
run;
You can now convert that to a macro with FORMULA as one of its parameters.
%macro mymacro(formula);
data want;
set sashelp.class;
age2 = &formula ;
run;
%mend mymacro;
Which you could then call like this to generate the same data step we started with.
%mymacro(formula=age ** 2)
But you could also pass something else. For example if you wanted AGE2 to be 21 for everyone you could just use:
%mymacro(formula=21)
Or perhaps you really want AGE2 to be a character variable.
%mymacro(formula="Age of Student")
Or perhaps
%mymacro(formula=put(age,Z3.))
So what data step do you want to run? Once you know that you can begin to try and make a macro.
For example for your macro if you wanted SOURCE_TIM to be set to 11 AM you might call it like this:
%m_test(indata=sashelp.class, outdata=want, dtvar=., timvar='11:00't);
So that it generates the statement:
SOURCE_TIM='11:00't ;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.