Create a time variable

Reply
Regular Contributor
Posts: 150

Create a time variable

I have 3 visits worth of data and I need to calculate time elapsed since last visit. Each of the visits have a date for when the interview was conducted. I am subtracting those dates to come up with the number of days between the interviews, however, it does not seem to be working correctly. My code is below and attached is a screenshot of the output. Suggestions as to how to fix the calculation would be greatly appreciated

Screen Shot 2014-12-02 at 8.29.40 PM.png

data visit2clean_sn;set visit2clean_s;

time=today6m-today();

run;

data visit3clean_s;

set visit3clean_s;

if today6m^=. then time=today12m-today6m;

else if today6m=. then time=today12m-today();

run;

Super User
Super User
Posts: 7,693

Re: Create a time variable

Well at an initial view you use a variable today12m which is not in the data you post above, it should be today.  You also don't need the else if:

if today6m^=. then time=today12m-today6m;

else time=today-today();

Regular Contributor
Posts: 150

Re: Create a time variable

There are two separate sets of code, one of the 6 month visit and one for the 12 month visit. I only provided sample output from the first code, attached here is the output from the second set of code but it isn't much different that I can see. Screen Shot 2014-12-03 at 9.47.22 AM.png

Super User
Posts: 11,107

Re: Create a time variable

What exactly does "however, it does not seem to be working correctly" mean? Values out of expected range? Sign incorrect? Format incorrect?

Provide some expected "correct" results.

What you have calculated are intervals in days. Is that what you want?

Regular Contributor
Posts: 150

Re: Create a time variable

I believe the values and signs are incorrect for some of the calculations but I could be reading the output incorrectly. In the output I posted today, obs 242, time= -49 but the difference between today12m and today is much greater than that. Is the negative value how SAS calculates time spans greater than 1 year?

This new variable will be the time covariate in a longitudinal analysis.

Super User
Posts: 11,107

Re: Create a time variable

Just for giggles, what do you see in the log if you run:

%put %sysfunc(today());

Regular Contributor
Posts: 150

Re: Create a time variable

I think I just figured out the problem actually, I remove the parenthesis from the end of today() and the calculations are correct. Now I am not sure what the parenthesis actually do and probably didn't need them in the first place. Attached in what I get in my log when I run the code above, thank you for your help.

Screen Shot 2014-12-03 at 10.45.30 AM.png

Super User
Posts: 11,107

Re: Create a time variable

Today() is a FUNCTION that calls todays date from the computer.

Ask a Question
Discussion stats
  • 7 replies
  • 228 views
  • 0 likes
  • 3 in conversation