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

I have created a macro variable using CALL SYMPUT in data step... I need to use that macro variable in another macro program in IF-ELSE condition with out using SYMGET... How to get the result?

 

my call symput program is

Data syed;
set sashelp.class;
if age >=13 then CALL SYMPUT('status','adult');
else CALL SYMPUT('stat','minor);
RUN;

 

I got the value for this macro variable using SYMGET function in data step.. -- This method is working fine

DATA ibbu;
SET sashelp.class;
if Age>=13 then status=SYMGET('status');
else status =SYMGET('stat');
RUN;

 

i need to achieve the same result in macro program in if else statement without using SYMGET... how to achieve this?

 

i tried this way... it didn't work for me...

%macro ibrahim;
data ibbu;
set sashelp.class;
%If Age>=13 %then %do; status= "&status" ; %end;
%else %if Age<13 %then %do; status= "&stat"; %end;
%mend ibrahim;
%ibrahim;

 

As per Condition it is not working.. It set all value of status to "adult". Can someone guide me on this one?

1 ACCEPTED SOLUTION

Accepted Solutions
_el_doredo
Quartz | Level 8
Sorry for the confusion here... I got my answer.. It's the percentage sign causing the issue for me... If i remove percentage sign in data step means it worked for me... Thanks for your response

View solution in original post

8 REPLIES 8
SASJedi
SAS Super FREQ

I can't see what you are hoping to accomplish with your macro code. Remember, MACRO programs are used to write DATA steps. It's the DATA step program that processes the data. Before the DATA step can run, the MACRO must have already finished writing it. So I'm not seeing what it is your macro code is trying to accomplish. Can you describe the result you want?

 

Check out my Jedi SAS Tricks for SAS Users
CarmineVerrell
SAS Employee

Hello,

Unfortunately, the way you are using macros will not work. Can you please be specific in what you are trying to accomplish? This will help in guiding you as the code you provide is confusing.

 

Just curious, have you thought of taking the SAS Macro Training?

 

 

_el_doredo
Quartz | Level 8
Sorry for the confusion here... I got my answer.. It's the percentage sign causing the issue for me... If i remove percentage sign in data step means it worked for me... Thanks for your response
Tom
Super User Tom
Super User

@_el_doredo wrote:
Sorry for the confusion here... I got my answer.. It's the percentage sign causing the issue for me... If i remove percentage sign in data step means it worked for me... Thanks for your response

Do you mean that you reverted you program just use normal IF/THEN statements instead of trying to use %IF/%THEN macro statements?

_el_doredo
Quartz | Level 8
Yeah exactly like this. I am in learning phase... but,this method worked for me

%macro ibrahim;
data ibbu;
set sashelp.class;
If Age>=13 then do; status= "&status" ; end;
else do; status= "&stat"; end;
%mend ibrahim;
%ibrahim;
CarmineVerrell
SAS Employee

I would highly recommend taking the SAS Macro Language 1: Essentials course as it will explain all the concepts that are confusing you and show you how to create a data-driven program. Great course and very useful.

Carmine

_el_doredo
Quartz | Level 8
Yeah sure.. It will be helpful for me to learn more about SAS macros.. Thanks for your suggestion
himself
Quartz | Level 8

Hi , this reminds me my first time to use Macro, I didn't understand the difference between %if -%then  and if- then

kindly find my findings:

1. '%if-%then' can ONLY be used in the MACRO program (whether inside the DATA step, or outside) and 'if-then' is ONLY used inside the DATA step (whether MACRO or non-MACRO). 

 

2.In the DATA step, you can use 'if alone to do logical comparison. But you can not use '%if alone. You also can not use '%else' alone without '%do; %end' pair. 

 

3.'%if-%then' uses MACRO variables in logical expressions and can not refer to DATA step variables in logical expressions (you can not write: %if x=l %then %do; because x is not a MACRO variable), while 'if-then' can use MACRO variables or use DATA step variables in logical expressions. 

 

4.'%if-%then' in MACRO determines what text should be copied to the input stack. When you use 'if-then' inside a MACRO (or non-MACRO), it does logical comparison to determine what DATA step statements should be executed. 

 

Hope this helps, thanks@_el_doredo

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Autotuning Deep Learning Models Using SAS

Follow along as SAS’ Robert Blanchard explains three aspects of autotuning in a deep learning context: globalized search, localized search and an in parallel method using SAS.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1796 views
  • 3 likes
  • 5 in conversation