BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
User12321
Obsidian | Level 7

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

 

View solution in original post

6 REPLIES 6
art297
Opal | Level 21

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

 

User12321
Obsidian | Level 7

Thanks a lot!
It worked this way.

I guess problem was that I did not use input for Date in comparision.

Reeza
Super User

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. 

ballardw
Super User

Did you see any notes about character values being converted to numeric or numeric to character in the log?

User12321
Obsidian | Level 7

Yew, there was some character conversion, and I thought it was related to data.

SASKiwi
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 559 views
  • 0 likes
  • 5 in conversation