DATA Step, Macro, Functions and more

Creating a variable from difference between two dates for use as end of a do loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 77
Accepted Solution

Creating a variable from difference between two dates for use as end of a do loop

[ Edited ]

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.


Accepted Solutions
Solution
‎01-27-2016 11:33 AM
Super User
Posts: 17,819

Re: Creating a variable from difference between two dates for use as end of a do loop

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;

 

View solution in original post


All Replies
Solution
‎01-27-2016 11:33 AM
Super User
Posts: 17,819

Re: Creating a variable from difference between two dates for use as end of a do loop

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;

 

Frequent Contributor
Posts: 77

Re: Creating a variable from difference between two dates for use as end of a do loop

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!

Super User
Posts: 10,500

Re: Creating a variable from difference between two dates for use as end of a do loop

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 216 views
  • 2 likes
  • 3 in conversation