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

Hi all,

 

Please help me have a look what is wrong with my code ? Thank you.

 

Each week, I have 3 date, then I need to select the latest available file - ga.cust_yyyymmdd from that 3 dates.

 

for example wk2 I have 3 date, 20190731, 20190730, and 20190729.

I need to pick a date,

if the file is available on 20190731, then I will use 20190731.

if 20190731 do not have file, then I will pick 20190730

if 20190730 also do not have file, then I will pick 20190729.

 

below is my code :

 

%put compute to testing valid date : &wk2_a. &wk2_b. &wk2_c. &wk0_a &wk0_b &wk0_c;

 

 

%macro date;

 

 

%if %sysfunc(exist(ga.cust_&wk2_a.)) %then %do;

%let wk2_dt=&wk2_a;

%end;

%else %if %sysfunc(exist(ga.cust_&wk2_b.)) %then %do;

%let wk2_dt=&wk2_b;

%end;

%else %if %sysfunc(exist(ga.cust_&wk2_c.)) %then %do;

%let wk2_dt=&wk2_c;

%end;

 

 

 

%if %sysfunc(exist(ga.cust_&wk0_a.)) %then %do;

%let wk0_dt=&wk0_a;

%end;

%else %if %sysfunc(exist(ga.cust_&wk0_b.)) %then %do;

%let wk0_dt=&wk0_b;

%end;

%else %if %sysfunc(exist(ga.cust_&wk0_c.)) %then %do;

%let wk0_dt=&wk0_c;

%end;

%mend;

%date;

 

 

below is the log:

 

%put compute to testing valid date : &wk2_a. &wk2_b. &wk2_c. &wk0_a &wk0_b &wk0_c;

compute to testing valid date : 20190731 20190730 20190729 20190724 20190723 20190722

1993

1994 %macro date;

1995

1996 %if %sysfunc(exist(ga.cust_&wk2_a.)) %then %do;

1997 %let wk2_dt=&wk2_a;

1998 %end;

1999 %else %if %sysfunc(exist(ga.cust_&wk2_b.)) %then %do;

2000 %let wk2_dt=&wk2_b;

2001 %end;

2002 %else %if %sysfunc(exist(ga.cust_&wk2_c.)) %then %do;

2003 %let wk2_dt=&wk2_c;

2004 %end;

2005

2006

2007 %if %sysfunc(exist(ga.cust_&wk0_a.)) %then %do;

2008 %let wk0_dt=&wk0_a;

2009 %end;

2010 %else %if %sysfunc(exist(ga.cust_&wk0_b.)) %then %do;

2011 %let wk0_dt=&wk0_b;

2012 %end;

2013 %else %if %sysfunc(exist(ga.cust_&wk0_c.)) %then %do;

2014 %let wk0_dt=&wk0_c;

2015 %end;

2016 %mend;

2017 %date;

2018

2019 %put final file date= &wk0_dt. &wk2_dt.;

WARNING: Apparent symbolic reference WK0_DT not resolved.

WARNING: Apparent symbolic reference WK2_DT not resolved.

final file date= &wk0_dt. &wk2_dt.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why are you trying to do the logic in macro code instead of just using normal code?

 

You also don't explain how the three dates are selected.  They look like three dates in a row, so perhaps you just need to know one of them?

 

%let date_wanted=20190731 ;
%let dsname=;

data _null_;
  date=input("&date_wanted",yymmdd8.);
  do date=date to date-2 by -1 until(found);
     dsname=cats('ga.cust_',put(date,yymmddn8.));
     found=exist(dsname);
  end;
  if found then call symputx('dsname',dsname);
run;

 

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Why are you trying to do the logic in macro code instead of just using normal code?

 

You also don't explain how the three dates are selected.  They look like three dates in a row, so perhaps you just need to know one of them?

 

%let date_wanted=20190731 ;
%let dsname=;

data _null_;
  date=input("&date_wanted",yymmdd8.);
  do date=date to date-2 by -1 until(found);
     dsname=cats('ga.cust_',put(date,yymmddn8.));
     found=exist(dsname);
  end;
  if found then call symputx('dsname',dsname);
run;

 

Gpp
Fluorite | Level 6 Gpp
Fluorite | Level 6

thank you very much and I will try it now.

Kurt_Bremser
Super User

What's basically wrong? You did not check if the issue at hand can be solved without the use of a macro.

This probably happens because your knowledge of the Base SAS tools (data step language and procedures) is still very limited.

So you should work (a lot) at that before engaging in macro programming.

 

The macro preprocessor is a text generator for creating dynamic SAS code. Without proper knowledge of the code to be created, it will be useless to you and only cause problems.

 

Macro variables have scope. If they are created in a macro, they are local to that macro and vanish as soon as the macro stops executing; only if they already existed in the global symbol table, or if you force them to be created there (either by the use of %global or "g" in call symputx) will they be available outside the macro.

 

But @Tom already showed you how to do it with data step tools.

Gpp
Fluorite | Level 6 Gpp
Fluorite | Level 6

yes, I know , my knowledge very limited that's why I am seeking help at communities. Thanks for letting me know why the thing doesn't work. Thank you.

Reeza
Super User
Are you searching for the latest version of a file?
Gpp
Fluorite | Level 6 Gpp
Fluorite | Level 6
Hi rezza, I tried tom’s method and it’s work, thank you 😃

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 3886 views
  • 0 likes
  • 4 in conversation