I am trying to do the following but I can not get the location begin_date or teacher to chance when the %let crsnum changes. the location begin_date and teacher are stuck with the same info.
/**picking a course number, print out the roster for the course with:
Location, start date and teacher's name in the title(s)
Number paid out of total registered and outstanding fees due in the footer***/
**Datasets used are mysas.register schedule courses**;
%let crsnum=13;
%macro ex1;
data revenue;
set mysas.register end=final;
where course_number=&crsnum;
total+1;
if paid='Y' then paidup+1;
if final then do;
put total= paidup=;
if paidup < total then do;
%let foot=Outstanding fees due;
end;
else do;
%let foot=Number paid out of total registered;
end;
end;
run;
%if &crsnum %then %do;
data _null_;
set mysas.schedule;
call symput('loc',location);
run;
%put &loc;
data _null_;
set mysas.schedule;
call symput('date',put(begin_date,date9.));
run;
%put &date;
data _null_;
set mysas.schedule;
call symput('teach',teacher);
run;
%put &teach;
%end;
Title "&loc &date &teach";
proc print data=mysas.register noobs;
where course_number=&crsnum;
footnote "&foot";
run;
%mend;
%ex1;
Why is this coded as a macro? There is no conditional logic that could not be handled via data step logic. The macro has no input parameters defined.
%let crsnum=13;
data revenue;
set mysas.register end=final;
where course_number=&crsnum;
total+1;
if paid='Y' then paidup+1;
if final then do;
put total= paidup=;
if paidup < total then call symputx('foot','Outstanding fees due');
else call symputx('foot','Number paid out of total registered');
end;
run;
data _null_;
set mysas.schedule;
if &crsnum then do;
call symputx('loc',location);
call symputx('date',put(begin_date,date9.));
call symputx('teach',teacher);
end;
run;
%put &=loc &=date &=teach;
%put &=foot;
title "&loc &date &teach";
footnote "&foot";
proc print data=mysas.register noobs;
where course_number=&crsnum;
run;
But shouldn't the IF test in the data _null_ step really be more like the WHERE statement in the first data step?
yes my code works but the location begin date and teacher doesn't change
In general, this is a pretty robust approach for creating a macro, but it does require working code.
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Just some quick comments on your code:
@chrissowden wrote:
I am trying to do the following but I can not get the location begin_date or teacher to chance when the %let crsnum changes. the location begin_date and teacher are stuck with the same info.
/**picking a course number, print out the roster for the course with:
Location, start date and teacher's name in the title(s)
Number paid out of total registered and outstanding fees due in the footer***/
**Datasets used are mysas.register schedule courses**;
%let crsnum=13;
%macro ex1;
data revenue;
set mysas.register end=final;
where course_number=&crsnum;
total+1;
if paid='Y' then paidup+1;
if final then do;
put total= paidup=;
if paidup < total then do;
%let foot=Outstanding fees due;
end;
else do;
%let foot=Number paid out of total registered;
end;
end;
run;
%if &crsnum %then %do;
data _null_;
set mysas.schedule;
call symput('loc',location);
run;
%put &loc;
data _null_;
set mysas.schedule;
call symput('date',put(begin_date,date9.));
run;
%put &date;
data _null_;
set mysas.schedule;
call symput('teach',teacher);
run;
%put &teach;
%end;
Title "&loc &date &teach";
proc print data=mysas.register noobs;
where course_number=&crsnum;
footnote "&foot";
run;
%mend;
%ex1;
There is nothing about your code that would ever change the value of those macro variables: &LOC, &DATE, and &TEACH.
They all come from the final observation in MYSAS.SCHEDULE.
That's probably not what you intended, but it's difficult to be sure. We have to rely on you to supply that piece.
Also note, %LET statements are never part of a DATA step. IF THEN statements do not affect whether or not they execute. If you want &FOOT to depend on conditions in the DATA step, use CALL SYMPUTX instead of %LET:
if paidup < total then do;
call symputx('foot', 'Outstanding fees due');
end;
They way the code stands now, you might as well take both %LET statements:
%let foot=Outstanding fees due;
%let foot=Number paid out of total registered;
Since they're not part of the DATA step, just place them before the DATA step begins (and remove them from the DATA step itself). That's actually the order in which the statements execute.
That's the section of the code that I know is not working correctly. The current code will keep the &loc &date &teach the same even if the course number changes. I want the macro variables to change when I choose a different course number
So how do you know which values you want to extract from MYSAS.SCHEDULE? If there is just one observation per course number, and if you want to select the values that match, you could add a WHERE statement:
data _null_;
set mysas.schedule;
where course_number = &crsnum;
call symput('teach',teacher);
run;
Why is this coded as a macro? There is no conditional logic that could not be handled via data step logic. The macro has no input parameters defined.
%let crsnum=13;
data revenue;
set mysas.register end=final;
where course_number=&crsnum;
total+1;
if paid='Y' then paidup+1;
if final then do;
put total= paidup=;
if paidup < total then call symputx('foot','Outstanding fees due');
else call symputx('foot','Number paid out of total registered');
end;
run;
data _null_;
set mysas.schedule;
if &crsnum then do;
call symputx('loc',location);
call symputx('date',put(begin_date,date9.));
call symputx('teach',teacher);
end;
run;
%put &=loc &=date &=teach;
%put &=foot;
title "&loc &date &teach";
footnote "&foot";
proc print data=mysas.register noobs;
where course_number=&crsnum;
run;
But shouldn't the IF test in the data _null_ step really be more like the WHERE statement in the first data step?
Why is this coded as a macro? There is no conditional logic that could not be handled via data step logic. The macro has no input parameters defined.
Because it's homework 🙂
it's not homework. The macro doesn't have to be there. I was just trying a few different ways. I just can't seem to get the names of the teachers and locations to change.
here is the log:
THanks... it was the where clause that fixed the problem. Thank you!!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.