BookmarkSubscribeRSS Feed
Pandu2
Obsidian | Level 7
Hi All,

Could anyone please help me in comparing two sas date variables which are in date9 formats only in data step.
For instance, sas_date >= old_date.

Any help would be appreciated, Thankyou.
67 REPLIES 67
andreas_lds
Jade | Level 19

If the variables are true date variables, the comparison operators will work as expected.

Pandu2
Obsidian | Level 7
Could you please provide me an example. When I try it on my side it gave me the blank data.
andreas_lds
Jade | Level 19

Please post the step you are using along with data in usable form.

Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Pandu2
Obsidian | Level 7
Data want;
Set have;
by id roll num;
If (first.id and first.roll and first.num)=1 and sas_date >=old_date
Then output;
run;
I sorted the dataset by using those 3 variables and using first keyword to get the 1st variable which should meet the condition sas_date>=old_date but it isn't working instead it is giving me a blank data
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Quentin
Super User

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.

 

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Pandu2
Obsidian | Level 7
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
PaigeMiller
Diamond | Level 26

@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

@Pandu2 

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.

--
Paige Miller
Quentin
Super User
You should trust that error message and add a PROC SORT step immediately
before your DATA step that sorts the data by all 3 variables, in the same
order as listed on your BY statement.
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Pandu2
Obsidian | Level 7
Thank you veryyy muchh, it worked nicely. I would like to add some more info to where condition in your code.
My data table has 6 variables named sas_date,old_date,id,roll,num, amount. I'm unable to add the dataset here, I strongly apologize for that.
After using your code, I closely monitored the output dataset, it met the where condition but it didn't take first.num value so for those cases Please do keep an or condition where if sas_date ge old_date fails and it unable to take the first.num then when there's a increase in amount field that row should be picked and the sas_date>=old_date should also match. Thankyou.
Kurt_Bremser
Super User

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.

Pandu2
Obsidian | Level 7
In this the if condition should apply first then after where condition should apply but it applied oppositely that's what I don't require. Please do make changes accordingly. Thankyou
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

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!

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
  • 67 replies
  • 1544 views
  • 1 like
  • 6 in conversation