I have a dataset that contains a start date and an end date. The end date column occasionally contains a "C" character, which supposedly stands for "current." I simply need to manipulate this value and change it to a date far in the future or even today's date. The format of the column is YYMMDDN8 Numeric. See screenshot below.
I am quite familiar with SQL, although outside of the context of SAS, and this seems to be a pretty simple solution using SQL code. I have tried the following statement shown in the log below.
However, as you can see from the error above, SAS does not want to compare the numeric type of the column with the 'C' character value, even though this column clearly contains that value. I have attempted changing the format of the column with PUT(), but I was still not able to get a case statement to work as formatted above.
Any help is appreciated.
Environment: SAS 9.4 for Windows
proc sql;
create table PERM.CEOTENURE5 as
SELECT *
,CASE
WHEN missing(dateEndRole) THEN today()
ELSE DateEndRole
END as DateEndRoleF
FROM PERM.CEOTENURE
;
quit;
Strip the formats and print a small sample to see what the underlying value is:
proc print data=dataname (obs=100);
var dateEndRole;
format dateEndRole;
run;
Running the following code
proc print data=PERM.CEOtenure;
var dateEndRole;
format dateEndRole;
run;
Returned
The only other thing I can think of, is that they're missing, with a missing show as C for some reason?
Try the missing function as your filter?
where missing(dateEndRole);
It looks like you are correct. Running this code successfully returns only 'C' values.
proc print data=PERM.CEOtenure;
var dateEndRole;
format dateEndRole;
where missing(dateEndRole);
run;
So in SQL, does this count as a NULL value? I still can't manipulate the data as I am used to. Where am I going wrong?
proc sql;
create table PERM.CEOTENURE5 as
SELECT *
,CASE DateEndRole
WHEN NULL THEN today()
ELSE DateEndRole
END as DateEndRoleF
FROM PERM.CEOTENURE
;
quit;
proc sql;
create table PERM.CEOTENURE5 as
SELECT *
,CASE
WHEN missing(dateEndRole) THEN today()
ELSE DateEndRole
END as DateEndRoleF
FROM PERM.CEOTENURE
;
quit;
@Reeza wrote:
The only other thing I can think of, is that they're missing, with a missing show as C for some reason?
Try the missing function as your filter?
where missing(dateEndRole);
Perhaps the missing option is set;
options missing='C';
or more likely it is special missing value .C
Probably to indicate continuing.
It appears possible that your data has been read in such a way the actual value is a special missing. If your raw data when read contains .C (please notice the . before the C) this could be the cause.
If that is the actual case you should be able to use
if dateendrole= .C then dateendrole = today();
Please see this brief example of a similar data.
data junk; input x; datalines; 1 3 .C 4 ; run; Proc print data=junk; run; data newjunk; set junk; if x=.C then x=-99; run; proc print data=newjunk; run;
The value is the special missing .C. You can test for .C or using the MISSING() function to find those values. You can replace them using IF/THEN logic or just use the COALESCE() function.
data perm.ceotenure2 ;
set perm.ceotenure;
dateendrolef = coalesce(dateendrole,today());
format dateendrolef yymmdd10. ;
run;
If you create a new variable the make sure to attach a date format to it so that it prints in human readable form. If you want to print in YMD format then YYMMDD10. is more legible or humans than YYMMDD8. since without the hyphens it looks like a number.
I ended up using coalesce in the original data pull and everything worked beautifully.
coalesce(A.DateEndRole, today()) as DateEndRole format=yymmddn8.
Thank you Rezza and Tom for your help!
It's probably the setting of your missing= option. Try this for reference:
options missing=C;
data test;
format mydate yymmddn8.;
mydate = today();
output;
mydate = .;
output;
run;
and then change the value of the option.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: