BookmarkSubscribeRSS Feed
raivester
Quartz | Level 8

Reading in my raw data set, I have a date variable that is character. An example of a date value is 20150127. I would like to create a new variable that has a date format. Among other things, I have attempted to do that in the following bit of code, but the example date I gave (20150127) gets transformed to 20110, which doesn't seem right. (Additionally, I think my WHERE statement or something is misplaced, because I ultimately end up with the error: ERROR: Variable DDI_d is not on file WORK.DATA_1. And the error:  ERROR: Variable DDI_d is not on file WORK.DATA_2. Can anyone help determine what's going on?

 

data data_final;
	merge data_1 data_2;
	by ID;
	if DDI ne "" then do;
		DDI_d = input (DDI,yymmdd10.);
	end;
	else do; 
		DDI_d="";
	end;
	where '1JUL2019'd<=DDI<='30JUN2020'd;
run;

 

2 REPLIES 2
Reeza
Super User
I suspect the value is correct, you just need to apply a format.
Add this line to your data step and re-run it.

format ddi_d yymmdd10.;
ballardw
Super User

Is DDI_d in Data_1? Where statements apply to values that already exist in the source data set(s). Your code does not show that variable on the Where statement shown so sort have to guess. Since you show DDI on your where statement I would expect the error to be more like:

5    data example;
6      set data_1;
7       where '1JUL2019'd<=DDI<='30JUN2020'd;
ERROR: WHERE clause operator requires compatible variables.
8    run;

because date literals are numeric and your DDI variable is stated to be character.

 

Best practice on this forum is when you have a question about an error, warning or note to copy from the LOG the entire data step or procedure that generates the message along with all error, notes and warning messages and then paste the whole thing into a text box opened on the forum using the </> icon at the top of the message box. The text box preserves the formatting of any of the diagnostic characters SAS often supplies with errors.

 

If you want to filter on what may be the newly created variable DDI_d then use:

IF '1JUL2019'd<=DDI_d<='30JUN2020'd;

 

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

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
  • 2 replies
  • 780 views
  • 0 likes
  • 3 in conversation