If the variables are true date variables, the comparison operators will work as expected.
Please post the step you are using along with data in usable form.
Please share the code you tried, and also tell us if there were any notes / warnings / errors in the log.
The code in your question, sas_date >= old_date , will work if these two variables exist in your data. Actually, it will work even if one or both of the variables doesn't exist, because SAS will create the variables for you.
So if it's not working, the problem is somewhere else in your code, or you're not understanding the values in the data.
People asked that you share (a portion of) the data as well. But you haven't done that. We're trying to help you but you have to help us too. We can't help you if we don't have your data.
Please provide the data in usable form, by typing it into SAS data step code, or by following these instructions.
Hi,
The code is syntactically correct, so it's likely a problem in your data, or your understanding of the code.
Your current code will select records with both first.num=1 and sas_date>old_date. If the first record for a group does not have sas_date>old_date, then no records will be selected from that group.
If your goal is to select the subset of records that have sas_date>old_date, and then select the first.num record from that subset, that would require different logic:
data want ;
set have ;
by id roll num ;
If first.num=1 ;
where sas_date >=old_date ;
run ;
The WHERE filtering happens before the data are read into the PDV and identifies the subset you are interested in, then first.num filtering is applied to that subset.
Note that the expression first.num=1 is exactly the same as (first.id and first.roll and first.num)=1 because if first.num=1, then both first.id and first.roll must be 1.
@Pandu2 wrote:
Thankyou for your assistance, this is the way I want to do. I used your code but it got an error like by variables are not properly sorted Even though they are sorted. Please advise me
Show us a portion of your data. We have advised you to do this several times already, we can't help you if we can't see your data. Please provide the data following these instructions.
Of course you can add a data step with datalines here, everyone can.
If data is sensitive, make up some fake values; what is important is the structure (variable types, lengths, formats), and a description of the expected result from the example dataset.
This is not rocket science, but a very valid skill which every SAS coder MUST have.
Agree with others. Please post some sample data that illustrates the problem. Ideally post the code with a DATA step with a cards statement that has maybe 10 records.
If you post sample input data, along with the code, and describe how the results you want differ from the results you are getting, people will be able to help you.
And often when you take the time to create a small sample input dataset, you end up solving the problem yourself. Because you immediately have an manageable example you can work with.
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.
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.