Hi dear members,
I have a column variable for the year of inspection since 1992 up to now stored as char $ 15.
1992
1993
.
.
.
2017
;
I would be grateful if anyone could help me to convert this to date(only year) and use it as variable for x-axis.
Thanks,
Create a new variable using the MDY() function.
INPUT() will convert it to a number first, avoiding notes in the log.
new_date = mdy(1, 1, input(year, 8.));
are you asking for this?
data have;
do _n_=1992 to year(today());
charyear=put(_n_,15. -l);
output;
end;
run;
data want;
set have;
numyear=input(charyear,best32.);
run;
Not sure what you mean by convert this to date(only year)
@novinosrinthank you for your reply.
The inspection year variable contains only the year and is stored as Char $15. . I want to change its type to date type. If I use the date type as a variable it shall be considered as date not char of numeric.
your code changed the char to numeric which is not the type I want.
Thank you!
Then you have a problem. There is no such thing as a date type in SAS. The only types of variables are character and numeric.
SAS does support calculations with dates, but those are numeric calculations. Dates are just another instance of a numeric variable.
Technically, dates in SAS refer to a specific day. It is not possible to have a date refer to an entire year.
The suggestion that @novinosrin gave you should be sufficient for using the year as an axis on a plot. If you try it an run into problems, post the log from your program.
Hi @mmhxc5,
If you need a time axis, you should create SAS date values, e.g., for 1 January of the year of inspection:
data want;
set have;
date=input(cats(charyear,'Q1'),yyq6.);
format date year.;
run;
(using novinosrin's dataset HAVE).
Then use DATE as the x variable, for example:
proc sgplot data=want;
series x=date y=date; /* replace y=date with something more interesting */
xaxis interval=year minor minorinterval=quarter label='Year of inspection';
run;
Create a new variable using the MDY() function.
INPUT() will convert it to a number first, avoiding notes in the log.
new_date = mdy(1, 1, input(year, 8.));
Thank you everyone for your time and help!
Appreciated!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.