I have a data set that has a variable for start_date of a semester and end_date of the same semester. Because the number of days between these two dates will vary semester to semester, I want to dynamically count how many dates occur between (including start & end) these two dates.
I couldn't find a good data set to use in the sashelp library - so I'll work on creating a data set that I can upload. Uploading the code I'm using hoping that the mistake is obvious to those significantly more skilled than myself.
data terms_temp_2;
/*retain used here solely for the purpose of arranging variables*/
/*for ease of reading*/
retain intermediateDate sas_cmsd_start_date sas_cmsd_end_date sas_term_start_date sas_term_end_date totalTermDays rawDayOfSem;
set terms_temp;
/*convert SQL DDMMMYYYY:HH:MM:SS to sas equivalent of Date only*/
sas_term_start_date = datepart(term_start_date);
sas_term_end_date = datepart(term_end_date);
sas_cmsd_start_date = datepart(cmsd_start_date);
sas_cmsd_end_date = datepart(cmsd_end_date);
/*calculate the difference between*/
totalTermDays = sas_term_end_date - sas_term_start_date;
/*create a variable from the above for use as "end" of a do loop*/
/*in this instance, there are 115 days between start & end*/
/*I want a dataset with an rawDayOfSem variable having values 1-115*/
call symputx("totalTermDays",totalTermDays);
%put &totalTermDays;
/*create a new observation with the number of days*/
/*since the start of the semester - First Day = 1*/
do i = 1 to &totalTermDays;
intermediateDate = sas_term_start_date + (i - 1);
rawDayOfSem = i;
output;
end;
run;
However, I get an error that apparent sympolic reference to TotalTermDays not resolved.
Thank you for any and all help.
Why not use the actual variable?
In general you can't use a macro variable in the same step you create it, but you can use the actual variable.
do i = 1 to totalTermDays;
Why not use the actual variable?
In general you can't use a macro variable in the same step you create it, but you can use the actual variable.
do i = 1 to totalTermDays;
I have no idea. I thought I tried that and it didn't work, so I went the other route.
Thank you so much! Just tested it and it works great!
Why are you trying to use the macro variable totalTermDays in a datastep Do loop when you have the perfectly valid variable totalTermDays available? Note that you are only going to have a value for the macro variable for the last record in your input set.
And your phrase "how many dates occur between (including start & end) these two dates" may not quite match what you have done.
Below is some simplier code, does not rely on an unsupplied example data set, and tests the behavior of 2 known dates for start and end. Does this do what you expected?
data test;
sas_term_start_date = '01JAN2016'd;
sas_term_end_date = '10JAN2016'd;
totalTermDays = sas_term_end_date - sas_term_start_date;
do i = 1 to totalTermDays;
intermediateDate = sas_term_start_date + (i - 1);
rawDayOfSem = i;
output;
end;
format sas_term_start_date sas_term_end_date intermediateDate mmddyy10.;
run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.