BookmarkSubscribeRSS Feed
rfarmenta
Obsidian | Level 7

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;

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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();

rfarmenta
Obsidian | Level 7

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

ballardw
Super User

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?

rfarmenta
Obsidian | Level 7

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.

ballardw
Super User

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

%put %sysfunc(today());

rfarmenta
Obsidian | Level 7

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

ballardw
Super User

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

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
  • 7 replies
  • 541 views
  • 0 likes
  • 3 in conversation