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

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

 

[...]


example.jpg
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
art297
Opal | Level 21

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?

 

x2PSx
Calcite | Level 5
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
Astounding
PROC Star

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.

x2PSx
Calcite | Level 5
Thank you, I wasn't aware of that fact. This simple code did it for me!
Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 2691 views
  • 1 like
  • 4 in conversation