I need to dynamically store a date value temporarily within a loop in SAS. When I run the code below outside of a macro it works.
proc sql noprint inobs=&i;
select date_var into :start from new_dataset;
quit;
%put Start Date: &start
However, when I run the code below it won’t work. Does anyone know what the issue is and a possible solution?
%macro loop_enrollment_first;
%do i = 1 %to 10;
proc sql noprint inobs=&i;
select date_var into :start from new_dataset;
quit;
%put Start Date: &start
%end;
%mend loop_enrollment_first;
%loop_enrollment_first;
3
Three points:
- I doubt this is the best way to solve your actual problem. This seems to be part of a larger problem, that can be solved much simpler.
- In your posted code, you must be sure that there are actually 10 obs available in your input data. Otherwise, the program will throw an error.
- I think the main problem here is the Stop Statement. You place it outside the If N = &i. part. Therefore, the program will exit the data step in the first iteration. You probably want to place the Stop Statement inside the If Block.
Take a look at this code and see if you can use it as a template
%macro loop_enrollment_first;
%do i = 1 %to 10;
%put &=i.;
data _null_;
p = &i.;
set sashelp.stocks point = p;
call symputx('start', date);
stop;
run;
%put Start Date: &start.;
%end;
%mend loop_enrollment_first;
%loop_enrollment_first;
3
You can read the i’th record using
proc sql noprint ;
select date_var into :start from new_dataset(firstobs=&i obs=&i);
quit;
The obs=
data set option should be mentally internalized as the lastobs specificier
The PROC SQL option INOBS controls the number of observations read in. But your SELECT … INTO statement is always going to select only the FIRST value, so the INOBS option does not do anything to change the behavior of the SELECT statement.
Are you just asking how to start reading a dataset from the Nth observation in a dataset?
You can use the FIRSTOBS= dataset option if that is what you are asking for. You can also use the OBS= dataset option if the goal is to read only that observation.
%macro loop_enrollment_first;
%local obsnum;
%do obsnum = 1 %to 10;
data _null_;
set sashelp.class(firstobs=&obsnum obs=&obsnum);
put "The values of observation number &obsnum are:" (_all_) (=/);
run;
%end;
%mend loop_enrollment_first;
If you are asking how to use a dataset to drive running the same code for each value then you could use CALL EXECUTE(). Make sure that you use %NRSTR() macro quoting to prevent any macro code you pass from trying to execute until after the data step finishes. Otherwise you get a very ugly SAS log and complex macros might not work properly because the macro logic decisions have to be made before the code the macro has generated has had a chance to execute.
data _null_;
set have;
call execute(cats('%nrstr(%mymacro)(date=',date_var,')'));
run;
or just write the generated code to a file and use %INCLUDE to run it.
filename code temp;
data _null_;
set have;
file code;
put '%mymacro(date=' date_var ')' ;
run;
%include code / source2;