BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GregG
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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;

 

GregG
Quartz | Level 8

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!

ballardw
Super User

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;

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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