Hello! Need your help.
I have to compare Dates, and based on Date assign data to 2 different data sets (using SAS 9.2).
Here is what I have:
%Let EffectiveDate = %sysfunc(compress(&NewPrice,,kd)); *Determine effective date of the most recent rate;
%put &effectiveDate;
No problem here, program prints for me EffectiveDate say 011517;
Then I read data set with original Dates as
Date1 :$10 from some text tab delimetered file. No problem.
I convert this Date1 to Date to match format of EffectiveDate:
Date2=input(Date1, mmddyy10.);
Date= put(Date2, mmddyy6.);
Now I need to divide data based on Date into 2 data sets:
data A B;
set abc;
ChRateDay = input("&EffectiveDate",mmddyy6.);
if Date < ChRateDay then output A;
else output B;
run;
I get all data in set A and nothing in set B. There are no errors in the log.
Original data set has data both prior and past "effective date"
What am I doing wrong?
Thanks.
From your description it's hard to tell what you are actually working with, but the following seems to mimic what you said, and produces the desired result:
%let EffectiveDate=011517; data abc; input date $10.; cards; 052516 020717 ; data A B; set abc; if input(Date,mmddyy6.) < input("&EffectiveDate.",mmddyy6.) then output A; else output B; run;
HTH,
Art, CEO, AnalystFinder.com
From your description it's hard to tell what you are actually working with, but the following seems to mimic what you said, and produces the desired result:
%let EffectiveDate=011517; data abc; input date $10.; cards; 052516 020717 ; data A B; set abc; if input(Date,mmddyy6.) < input("&EffectiveDate.",mmddyy6.) then output A; else output B; run;
HTH,
Art, CEO, AnalystFinder.com
Thanks a lot!
It worked this way.
I guess problem was that I did not use input for Date in comparision.
Your dates are character strings and the comparisons won't work. If you use YYMMDD then it will sort correctly.
Or you can use SAS dates, which are numeric and then apply the relevant formats. This is the solution Art has proposed and probably the best.
Did you see any notes about character values being converted to numeric or numeric to character in the log?
Yew, there was some character conversion, and I thought it was related to data.
Such notes should be fixed by correcting the data types of the variables involved - while @art297's fix works you shouldn't need to convert date variables to compare them. Storing them as SAS dates to begin with would be considered best practice and will simplify your coding.
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!
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.
Ready to level-up your skills? Choose your own adventure.