BookmarkSubscribeRSS Feed
Fluorite | Level 6



I am trying to create a new cross-sectional age variable from the existing longitudinal age variable based on the patient's diabetes status during 10 visits.

If the patient is non-diabetic, pre-diabetic or unknown through all the visits then age of patient will be the last age recorded, if the patient is diabetic at visit 5 then age of patient will be age when the patient was first recorded as diabetic (i.e. age at visit 5)


Here's what i attempted.
if diab in (1,3,4) then cross_age=last.age;
else if diab=2 then cross_age=first.age;



Super User

First thing, post your entire data step.

Second, please post code into either a text box opened with the </> icon or a code box opened with the "running man" icon that appears above the message window.


You use of First.age and Last.age looks like you may have tried something with by group processing, which is likely the correct approach. But the SAS FIRST and LAST variables only ever take values of 1 (the record is a first or last value for a variable) or 0 (not first or last). So you almost certainly do not want the value "first.age".

We need the entire data step to see just what By statement you may actually have used.


Also you should provide an example of what values you have for a couple of "patients", fake is fine as long as the behavior is the same AND provide what you expect the result for that example to look like. Data should be provided as a data step something like:

data have;
   input id $ age visit diab;
abc 25 1 1
abc 26 2 1
abc 28 3 1
abc 29 4 2

"visit" could be a date or what ever you have to identify the specific visit.

Also you should make sure we know which value of diab means what.

Fluorite | Level 6


 Thanks @ballardw 

The visits are set for all patients to 10 hence there is no "visit" variable in the dataset.

Here's an example of the data:

data have;
   input id $ age diab;
abc 25 1
abc 26 1
abc 28 1
abc 29 2
abc 30 2
abc 32 2
abc 33 2
abc 35 2
abc 36 2
abc 37 2
bcd 22 1
bcd 23 1

For the data step, this was the only code I tried.

data experiment;
set have;
by age;
if diab in (1,3,4) then cross_age=last.age;
else if diab=2 then cross_age=first.age;

value of diab:






Super User

What does the expected output look like? Really we need to know. This is a small enough sample you should be able to eyeball the data provide the expected result.


You BY needs to include ID as your data is not sorted by age and would throw an error about data not sorted. Read the log.

Since none of your "example" values show repeats of age within an ID then every age is "first".


Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


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
  • 2 in conversation