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

Hello

I want to create a macro variable called FromDate that will get the following value:

IF data set ttt exists then it will get value of max(BirthDate) 

IF data set ttt doesn't exist then it will get value 22158 (Which represent 31Aug2020)

 

What is the way to do it please? 

The code below is not working well, since data set ttt exists I expect to get value 22428 but I get value 22158.

Why?

 


data ttt;
input BirthDate : date9.;
cards;
15Aug2020'd
28MAY2021'd
;
Run;

%macro RRR;
%if %sysfunc(exist(ttt)) %then %do;
PROC SQL noprint;
select  max(BirthDate) into : FromDate	   
from  ttt
;
QUIT;
%end;
%else %do;
data _null_;
call symputx('FromDate','22158');
run;
%end;
%mend RRR;
%put &FromDate;

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

untested

%macro narf;
  %global FromDate;
  %if %sysfunc(exist(work.ttt)) %then %do;
    proc sql noprint;
      select max(BirthDate) into :FromDate trimmed
        from work.ttt;
    quit;
  %end;
  %else %do;
    %let FromDate = '31Aug2020'd;
  %end;
%mend;

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

untested

%macro narf;
  %global FromDate;
  %if %sysfunc(exist(work.ttt)) %then %do;
    proc sql noprint;
      select max(BirthDate) into :FromDate trimmed
        from work.ttt;
    quit;
  %end;
  %else %do;
    %let FromDate = '31Aug2020'd;
  %end;
%mend;
ballardw
Super User

Where is this macro variable to be used?

 

You may run into scope issues of where this is defined not being available to where actually wanted.

Ronein
Onyx | Level 15

What is wrong with my code?

Astounding
PROC Star
Three places to begin...
Does your program actually call the macro? I would expect to see this statement: %rrr
Second, for testing purposes, simplify the macro down to a one line version. After creating ttt:
%macro rrr2;
%put %sysfunc(exist(ttt));
%mend rrr2;
%rrr2
Third, is your post showing the actual definition of %rrr or a simplified version? We need to see the actual first line (%macro %rrr statement).

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1564 views
  • 1 like
  • 4 in conversation