BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10
data have;
x='2018-10-01 00:00:00:00';
y=input(x,anydtdtm.);

z=datepart(y);
put z;
format z YYMMDDN8.;
zz=put(z,YYMMDDN8.);
run;

In this example zz is converted to a character from a date format.  In my actual data when I use the above routine it stays as a date.  It will not change from a date to a character.  Is there some other way to convert from date to character?

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

If you want a character date there is no need to convert it to a SAS date:

data have;
  x = '2018-10-01 00:00:00:00';
  zz = compress(substr(x, 1, 10), '-');
  put _all_;
run;

View solution in original post

2 REPLIES 2
SASKiwi
PROC Star

If you want a character date there is no need to convert it to a SAS date:

data have;
  x = '2018-10-01 00:00:00:00';
  zz = compress(substr(x, 1, 10), '-');
  put _all_;
run;
ballardw
Super User

 


@Q1983 wrote:

 

Is there some other way to convert from date to character?

Once a variable type is assigned to a variable name in a SAS data set then the type cannot change. If you want to reuse the variable name with a new type then you basically rename the old variable to something else (or drop from the data set) and the create a new variable of the desired name.

 

You will find lots of examples on the forum related to fixing proc import created inconsistent variable types that look something like this:

data new;
   set old (rename=(thisvar=oldnumvar);
   thisvar = put(oldnumvar,z15.);
run;

The format changes, or sometimes an INPUT function is called with an old character variable to create numeric.

There might follow a call to Proc Datasets to delete the prior dataset "old" and rename "new" to "old" to reuse the same data set name.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 266 views
  • 1 like
  • 3 in conversation