BookmarkSubscribeRSS Feed
epibio2021
Obsidian | Level 7

Hi SAS users, 

 

My dataset has 35 variables that are in SAS datetime format, for ex: 

16APR2019:21:22:00
 
I am trying to check the chronological order of these variables, where var1 occurred before var2, var 2 before var3, ...var34 before var35 for all 35 variables. For each variable, there is either a datetime value or a missing value. This needs to be done for each case ID. 
 
So for case #1, I need to check that var1 occurred before var2, var 2 before var3,...var 35. Repeat for all cases. My goal is to flag observations if the events are not in chronological order. I am a beginner and having difficulty approaching how to do this.
 
Thank you, any help would be appreciated. 

 
7 REPLIES 7
Jagadishkatam
Amethyst | Level 16
could you please post sample data.
Thanks,
Jag
epibio2021
Obsidian | Level 7

Sample Data: 


Obsvar1var2var3
104MAY2018:13:30:0004MAY2018:18:35:0006MAY2018:14:07:00
204MAY2018:17:00:0005MAY2018:00:43:00.
309MAY2018:06:55:0009MAY2018:12:00:0014MAY2018:11:23:00
412MAY2018:20:00:00.14MAY2018:17:30:00
514MAY2018:14:00:0014MAY2018:17:50:0015MAY2018:11:37:00

 

For each obs, I need to check that var 1 occured before var 2, and var 2 occured before var 3. 

Tom
Super User Tom
Super User

What is the answer you want for that example input?

ballardw
Super User

What does the result look like?

Do you want one variable that indicates "all in order"/"not in order" or do you want 34 variables showing the comparisons of var1 to var2, var2 to var3, var3 to var4, ..., var34 to var35?

And HOW are missing values actually treated? Ignored in comparisons? Always "out of order"? Out of order if other than the first (or last or some other rule)?

 

SAS will allow you to compare multiple variables at one time like:  v1 < v2 < v3 <v4< ...<v35.

But if you use this the missing value is always less than anything. So any missing other than in the left most variables the result for the total comparison will be "false". So how missing is to be treated needs to be stated explicitly. If you want pairwise comparison results then the rule for how missing needs to be provided as well as you have 3 cases to consider involving missing:

Var1 missing , Var2 not missing

Var1 not missing, Var2 missing

Var1 missing , Var2 missing

So rules involving all of these cases would be needed.

 

epibio2021
Obsidian | Level 7

I would like one variable to indicate that chronological order is "good" or "not good". 

 

I currently have the following: 

if var1 <= var2 <= var3 .... <= var35 then CHRON = 'Good'; 

As you've stated, if any of the 35 variables is missing data, CHRON returns a missing value. I would like any missing data to be ignored and skip to the next variable. 

So if var2 and var5 are missing data, I would like it to check that var1<var3<var4<var6 = good/not good. 

 

Thank you. 

mkeintz
PROC Star

The program below compares neighboring variables.  If the, say, 6th variable is less than the 5th, then flag=6.  If all 35 variables are in non-decreasing order, then flag=0.

 

data want;
   set have;
   array dtimes {*}   var1-var35;
   do flag=2 to dim(dtimes) until(dtimes{flag}<dtimes{flag-1});
   end;
   if flag=dim(dtimes)+1 then flag=0;
end;

Note that missing date values compare as less than all non-missing data values.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
dikang77
Calcite | Level 5
Works for me! Thanks!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2640 views
  • 3 likes
  • 6 in conversation