BookmarkSubscribeRSS Feed
AwesomeSAS
Calcite | Level 5

Please help me to write a code to check if a variable is in sequential order for each vaccination. I am a new SAS programmer.

3 REPLIES 3
Tom
Super User Tom
Super User

@AwesomeSAS wrote:

Please help me to write a code to check if a variable is in sequential order for each vaccination. I am a new SAS programmer.


Which variable?  What does a "vaccination" mean? 

What do you mean by sequential order?

 

Assuming that you mean you have some variable, let's call it VAX, that defines groups of observations and you want to check if some other variable, let's call it OTHER, is monotonically increasing by one for each observation in the group then you probably want something like:

data want;
  set have;
  by VAX ;
  diff = dif(other);
  if first.vax then diff=.;
  else if diff ne 1 then put 'WARNING: Non sequential value. ' _n_= VAX= OTHER= DIFF= ;
run;

 

Patrick
Opal | Level 21

You need to describe what you have and what you want a bit more and also provide some sample data if you're after code.

It's normally also appreciate if you share some of the code you already tried whether it's working or not. Such code still helps us to understand your thinking and on what coding level you are so we can give you answers on an appropriate level.

 

I couldn't resist to copy/paste your question into chatGPT. It didn't return valid code but it did create sample data and the code was close enough for a quick fix. 

data vaccinations;
  input patient_id vaccination_date:yymmdd10.;
  format vaccination_date date9.;
  datalines;
1 2023-01-01
1 2023-02-01
1 2023-03-01
2 2023-01-15
2 2023-02-15
3 2023-01-10
3 2023-02-10
3 2023-02-09
3 2023-03-10
;

data check_sequential;
  set vaccinations;
  by patient_id;
  sequential_order_flag= ( first.patient_id or vaccination_date >= lag(vaccination_date) );
run;

proc print data=check_sequential;
run;

Patrick_0-1703806289917.png

 

To populate the flag variable above code takes advantage that SAS returns 1 if a logical expression is TRUE and 0 if it's FALSE.

Patrick_0-1703806767873.png

The expression becomes TRUE if it's a new patient_id - FIRST.patient_id - OR if the vaccination_date is greater or equal compared to the vaccination_date of the previous row - LAG(vaccination_date)

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 699 views
  • 1 like
  • 4 in conversation