Desktop productivity for business analysts and programmers

If Statements inside a macro to create a macro variable

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

If Statements inside a macro to create a macro variable

Hello, I am very new to SAS EG (about a month now) and I have this specific problem inside one of my macros:

So I have this table "STRESS_1.BETACOEFFS_TCE_RWA" that contains only one row of many variables. I want to create a macro variable "&BETA." that takes the value of 1 if the variable "_DEPVAR_" in my table is called "TCE_RWA_01_Beta", or has "01_Beta" in the name (both works out for me) and the value of 0 if not (AKA if it is called "TCE_RWA" or doesn't contain "01_Beta"). Now, I know for a fact that the small code below should give the &BETA. macro the value of 1, since it is what I read and yet I always get the value of 0 with the %put statement and in my results of my bigger macro. I think the problem comes from the if statement. I tried many many different things but I just can't figure it out. I need some help, thanks in advance!

 

[...]

 

data _null_;

     set STRESS_1.BETACOEFFS_TCE_RWA;

     %if _DEPVAR_ = TCE_RWA_01_Beta %then %do;

     %let BETA=1;

     %end;

     %else %do;

     %let BETA=0;

     %end;

run;

 

%put TCE_RWA_01_Beta should be 1 but is &BETA.;

 

[...]

Attachment

Accepted Solutions
Solution
‎06-21-2017 09:15 AM
Respected Advisor
Posts: 4,969

Re: If Statements inside a macro to create a macro variable

DATA steps are not capable of executing macro language statements.  All of the statements you used there are not part of the DATA step at all. 

 

There are a limited number of DATA step statements that function as an interface between the DATA step and macro language.  Here is an example of what you might do:

 

%let beta=0;

data _null_; 

     set STRESS_1.BETACOEFFS_TCE_RWA;

     if index(upcase(_DEPVAR_), '01_BETA') then call symputx('beta', 1);

run;

 

Assuming that your data set contains just one observation as you indicated, that should give &BETA the proper value.

View solution in original post


All Replies
Esteemed Advisor
Posts: 7,290

Re: If Statements inside a macro to create a macro variable

Is that code inside a SAS macro (i.e., SAS code that starts with %macro somename; and ends with $mend;

 

or is it just open code?

 

New Contributor
Posts: 3

Re: If Statements inside a macro to create a macro variable

Yes it is inside of my MACRO with the MEND as well at the end. I've done macros before this is not the problem, it's my if statement that doesn't register. Thank you
Solution
‎06-21-2017 09:15 AM
Respected Advisor
Posts: 4,969

Re: If Statements inside a macro to create a macro variable

DATA steps are not capable of executing macro language statements.  All of the statements you used there are not part of the DATA step at all. 

 

There are a limited number of DATA step statements that function as an interface between the DATA step and macro language.  Here is an example of what you might do:

 

%let beta=0;

data _null_; 

     set STRESS_1.BETACOEFFS_TCE_RWA;

     if index(upcase(_DEPVAR_), '01_BETA') then call symputx('beta', 1);

run;

 

Assuming that your data set contains just one observation as you indicated, that should give &BETA the proper value.

New Contributor
Posts: 3

Re: If Statements inside a macro to create a macro variable

Thank you, I wasn't aware of that fact. This simple code did it for me!
Esteemed Advisor
Posts: 6,646

Re: If Statements inside a macro to create a macro variable

Since the text (!) _DEPVAR_ is never equal to the text TCE_RWA_01_Beta, the %if condition evaluates to false, and macro variable beta is set to 0.

Keep in mind that the macro processor is a PREprocessor and macro statements are resolved before the data step is even compiled.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 147 views
  • 1 like
  • 4 in conversation