BookmarkSubscribeRSS Feed
lzanotti
Calcite | Level 5

Hi Community, 

 

I am quiet new to SAS Macro world and I am trying to achieve the following:

Given a controller dataset (image attached luca.ds) I would like to loop through the observation and check if m1date is today (potentially also m2date). If the condition is met, I would like to store the cost_centre and email, and use it to create partitions based on cost_centre from a master dataset with that names and send automated emails containing those.

I am writing the following code but as per log attached it seems is not getting any record and I do not know where I am wrong. If I increase the number of datasetCount it fetch the last record but not the first. 

thanks in advance for your precious help

Luca

%macro loopOverDatasets();

%local datasetCount iter inLibref inMember;
%let todaysdate=%sysfunc(today(),date9);
/*get number of records*/
proc sql noprint;
select count(*)
into :datasetCount
from luca.ls;
quit;

/*initiate loop*/
%let iter=1;

%do %while (&iter.<= &datasetCount.);
/*get dates and if equal to today costcentre for dataset you will work on during this iteration*/
%put &datasetCount;
%put &iter;
%put &todaysdate;
data _NULL_;
set luca.ls (firstobs=&iter. obs=&iter.); *only read 1 record;
*write the dates to the macro variables;
call symput("indate",put(m1date,date9.)); 
call symput("indate2",put(m2date,date9.));
%put &indate ***e**;
%put &indate2 ***d**;
%if %sysevalf("&indate."d = "&todaysdate."d) %then %do;
call symput("inLibref",Cost_Centre);
%put &inLibref ***b**; 
call symput("inMember",email);
%put &inMember;
%end;
run;





/*now you can apply your logic to the dataset*/
/*data &inLibref..&inMember.; *assuming you want to apply the changes to the dataset itself;
set work.trans2;
where cost_centre=&inLibref.;
/*** INSERT YOUR LOGIC HERE ***/
/*run;

/*** ANY OTHER PROCS/DATA STEPS ***/
/*just remember to use &inLibref..&inMember. to refer to the current dataset*/

/*increment the iterator of the loop*/
%let iter=%eval(&iter.+1);
%end;
%mend;

/*call the macro*/
%loopOverDatasets()
1 REPLY 1
PaigeMiller
Diamond | Level 26

Use an ARRAY to loop over variables in an observation.

 

Macros are not needed, macros complicate the programming tremendously.

 
--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 820 views
  • 0 likes
  • 2 in conversation