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

Can I use %let statement conditionally outside of macro? Do I always have to define macro first and put %let inside if I want to use this with a certain condition?

 

For example, can I modify the second sentence so that it only works when x is equal to one? Then, the second sentence wouldn't work if 2 is given to x.

 

%let x= 1;
%let y= &x;

 

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@braam:

If your SAS version/release doesn't yet allow open code macro statements mentioned by @Astounding, you can do:

data _null_ ;                                 
  if "&x" = "1" then call symputx ("y", "1") ;
run ;                                         

but it's hardly better than using a macro. Or if the macro variable Y already exists, you can do a simpler:

%let y = %sysfunc (ifc (&x=1, 1, &y)) ;

  If Y doesn't yet exist and X is not 1, it will result in a recursive error. But if you want Y to become 1 if X=1 and null otherwise regardless of whether Y already exists or not and/or its existing value, you can do:

%let y = %sysfunc (ifc (&x=1, 1,)) ;

Kind regards

Paul D.

View solution in original post

3 REPLIES 3
Astounding
PROC Star

The capability you are asking about depends on the release of SAS that you are using.  In recent updates to the software, you would be allowed to do this without having to define a macro:

 

%let x = 1;
%if &x=1 %then %let y = &x;
Tom
Super User Tom
Super User

As of SAS 9.4M5 released there is limited support for %IF/%THEN statements in open code. You must use %DO/%END block.

%let x= 1;
%if &x=1 %then %do;
  %let y= &x;
%end;

You can  always use a data step for creating a macro variable.

data _null_;
  if symget('x')='1' then call symputx('y',symget('x'));
run;

You can use the IFN (or IFC) function with %SYSFUNC(), but then you would need to know what you want to set Y to when X is not 1.

 

hashman
Ammonite | Level 13

@braam:

If your SAS version/release doesn't yet allow open code macro statements mentioned by @Astounding, you can do:

data _null_ ;                                 
  if "&x" = "1" then call symputx ("y", "1") ;
run ;                                         

but it's hardly better than using a macro. Or if the macro variable Y already exists, you can do a simpler:

%let y = %sysfunc (ifc (&x=1, 1, &y)) ;

  If Y doesn't yet exist and X is not 1, it will result in a recursive error. But if you want Y to become 1 if X=1 and null otherwise regardless of whether Y already exists or not and/or its existing value, you can do:

%let y = %sysfunc (ifc (&x=1, 1,)) ;

Kind regards

Paul D.

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!

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
  • 3739 views
  • 5 likes
  • 4 in conversation