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

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.));

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

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)

mmhxc5
Quartz | Level 8

@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!

Astounding
PROC Star

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.

FreelanceReinh
Jade | Level 19

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;
Reeza
Super User

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.));
mmhxc5
Quartz | Level 8

Thank you everyone for your time and help!

Appreciated!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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