BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

 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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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 ;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Why are you dropping all of the other variables?

stataq
Quartz | Level 8

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. 😳

Tom
Super User Tom
Super User

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 ;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 3 replies
  • 547 views
  • 0 likes
  • 2 in conversation