BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Susanknoll727
Calcite | Level 5

Hi All,

I am working on a dataset in SAS Studio, which I have never worked on before. I have VISITDATE and DOB variables and AGEATVISIT (all numeric). There are a handful of the AGE that are 0 that I would like to calculate the age for. Below is my code and Log included. What is wrong with my code? Thank you! 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The Error  you get is pretty clear, you have a / in the code where it doesn't belong. So nothing was actually done.

 

Additionally, the 'dtday' value for interval in INTCK or INTNX function would be to use the date part of a DATETIME values, which are stored as numbers of seconds.

Unless you did something pretty unnatural with the date vales shown in your CSV, which appear to be mm/dd/yyyy values such as VisitDate = 10/20/2030 and should be DATE values (number of days).

 

It also would be returning the number of day boundaries between the dates.

If you want an age in years the interval would be 'year' for date values. However the way that INTCK works the result of a 'year' when the start date is 31DEC2020 and the end date is 01JAN2021 by default is 1 because the year boundary occurs between the two dates. To use Intck to get reasonable age in years the way most of count age you add the method='C' (or 'Continuous') so the length of a year is considered, not the date of the year boundary.

 

data example;
   age = intck('year','17JUN1987'd,'20APR2030'd,'C');
run;

You may want to share the results of running Proc Contents on your GRC1 data set to confirm the variable types, informats and formats.

View solution in original post

14 REPLIES 14
PeterClemmensen
Tourmaline | Level 20

Seems you did not post your code or log?

Susanknoll727
Calcite | Level 5

Hi Peter,

 

Thought I did. Let me try again!

 

Thanks!

 

ballardw
Super User

@Susanknoll727 wrote:

Hi All,

I am working on a dataset in SAS Studio, which I have never worked on before. I have VISITDATE and DOB variables and AGEATVISIT (all numeric). There are a handful of the AGE that are 0 that I would like to calculate the age for. Below is my code and Log included. What is wrong with my code? Thank you! 


Include some of the values of Visitdate and DOB that have this result.

If using INTCK and either of the date values used is missing OR contains a numeric value that is not a valid date the result would be missing.

Susanknoll727
Calcite | Level 5

I have attached the dataset - it is fake data. Lines 31, 65, and 99.

 

Thank you! 

ballardw
Super User

And your code / log is ?

It should be fairly simple to copy text from the editor or log window. Then on the forum open a text box using the </> icon that appears above the main message window and paste the text.

Susanknoll727
Calcite | Level 5

I have loaded the code and log into the "running man" box twice and clicked ok. Let me try again. Not sure what I am doing wrong. First time I have posted here. So sorry. 

Data GRC2;
     Set GRC1;
     
/* AgeAtVisit calculation if not provided*/
  / if AgeAtVisit= 0 then AgeAtVisit = intck(‘dtDay’,VisitDate,DOB);
run;
 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         Data GRC2;
 70              Set GRC1;
 71         
 72         /* AgeAtVisit calculation if not provided*/
 73           / if AgeAtVisit= 0 then AgeAtVisit = intck(‘dtDay’,VisitDate,DOB);
              _
              180
 ERROR 180-322: Statement is not valid or it is used out of proper order.
 
 74         run;
 
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set WORK.GRC2 may be incomplete.  When this step was stopped there were 0 observations and 13 variables.
 WARNING: Data set WORK.GRC2 was not replaced because this step was stopped.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              918.65k
       OS Memory           24236.00k
       Timestamp           08/07/2024 02:45:38 PM
       Step Count                        121  Switch Count  0
       Page Faults                       0
       Page Reclaims                     116
       Page Swaps                        0
       Voluntary Context Switches        0
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           8
       
 
 75         
 76         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 86         
 User: u63971301

 

ballardw
Super User

The Error  you get is pretty clear, you have a / in the code where it doesn't belong. So nothing was actually done.

 

Additionally, the 'dtday' value for interval in INTCK or INTNX function would be to use the date part of a DATETIME values, which are stored as numbers of seconds.

Unless you did something pretty unnatural with the date vales shown in your CSV, which appear to be mm/dd/yyyy values such as VisitDate = 10/20/2030 and should be DATE values (number of days).

 

It also would be returning the number of day boundaries between the dates.

If you want an age in years the interval would be 'year' for date values. However the way that INTCK works the result of a 'year' when the start date is 31DEC2020 and the end date is 01JAN2021 by default is 1 because the year boundary occurs between the two dates. To use Intck to get reasonable age in years the way most of count age you add the method='C' (or 'Continuous') so the length of a year is considered, not the date of the year boundary.

 

data example;
   age = intck('year','17JUN1987'd,'20APR2030'd,'C');
run;

You may want to share the results of running Proc Contents on your GRC1 data set to confirm the variable types, informats and formats.

Susanknoll727
Calcite | Level 5

Thank you . Does this mean I would code for those three observations?

ballardw
Super User

@Susanknoll727 wrote:

Thank you . Does this mean I would code for those three observations?


Modify your existing code

/ if AgeAtVisit= 0 then AgeAtVisit = intck(dtDay,VisitDate,DOB);

to

if AgeAtVisit= 0 then AgeAtVisit = intck('year',VisitDate,DOB,'C');

 

Note your existing code has a second problem which may indicate you copied the DTDAY option from some other source and that is the presences of the curly or "smart" quotes. Please note the highlighted quotes in the first code that I colored red and made large. Those are not acceptable in SAS code for use in quoting. You have to replace them with the typed quote or double-quote character from your keyboard. Copying code from websites or files opened in anything except plain text or programming editors is the most likely source of the incompatible characters. So watch out for such.

 

Note: Saying "doesn't work" or similar really requires showing the LOG from what didn't happen. Otherwise we have no idea what code was submitted or what issues SAS may have objected to.

Tom
Super User Tom
Super User

Your variables have DATE values (or at least they better since you have elected to display them with the MMDDYY format which only works correctly on date values).

 

So to get the number of days, which is what it looks like you wanted to try to do, you can just subtract the two date values.  Date values are stored in days.

 

Do you really want AGE calculated in DAYS?

antonbcristina
SAS Employee

Hi @Susanknoll727, from the log you posted it looks like SAS is trying to draw your attention to the extra / in from of the IF statement. Remove that and run your program again.

 

 73           / if AgeAtVisit= 0 then AgeAtVisit = intck(‘dtDay’,VisitDate,DOB);
              _
              180
 ERROR 180-322: Statement is not valid or it is used out of proper order.

 

Susanknoll727
Calcite | Level 5

Hi Anton,

 

Thank you. I removed that. Unfortunately that did not fix the problem. 

antonbcristina
SAS Employee

If you could repost your log once you run the new code and a few observations, it would help us to detect the issue. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 14 replies
  • 1741 views
  • 0 likes
  • 5 in conversation