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

My current date character value is stored as ddmmmyyyy so I used date9. when changing it to the numeric value. The problem is that when I look at my data set the value for that variable is the SAS date. I tried using "format date date9.;" to convert the SAS date to Date 9. but I am getting the following error message: ERROR 48-59: The format $DATE was not found or could not be loaded.
What am I doing wrong? Thanks.

 

My code:

format date date9.;
datel=input(date,date9.);

totday=intck('day',daterand,date);

 

 

example of data: 

Date                   Subject #                Visit #           Randomization date

01May1996            1                           1                        15Jan1996

25July1996             1                           2                        15Jan1996

15Aug1995             2                          1                          20Mar1995

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

In my example:

data want;
  set have (rename=(date=char_date));
  date=input(char_date,date9.);
  format date date9.;
run;

I first rename the variable you already have - in the set statement - so no variable named date is in the data at that moment.  Now when I use the assingment date=, this creates a new variable called date - which as I have not specified a length/type, defaults to numeric 8.  Into this new variable I then put the result of the input() function, and its this new variable which is formatted.  

View solution in original post

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post example test data as a datastep, and show what the output should look like.  If you have a character field, then you can't just format it to date as it will still be a chacter variable.  Do something like:

data want;
  set have (rename=(date=char_date));
  date=input(char_date,date9.);
  format date date9.;
run;

So your creating a numeric variable to hold a numeric date.

PaulaC
Fluorite | Level 6
Thank you for your help. Doesn't the input function convert it to a numeric date?
I will most definitely try to post my example data as a datastep. I always seem to run into errors when I do that especially with dates.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, the input function returns a number - which is the number of days since a certain date which is how SAS keeps dates.  If you have:
<character variable>=112344;

format <character variable> <numeric format>;

Its not going to work.  So you want the variable that recieves the number from the input() function to be number, so it can be formatted using the numeric format date9.

 

This should help on the dataset:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

PaulaC
Fluorite | Level 6
Thank you. One more question regarding this issue. Does the variable name need to be different in the following statement: date=input(char_date,date9.); Can you keep the same variable name but permanently change the variable type from character to numeric? I noticed that once I changed the variable name then it worked.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

In my example:

data want;
  set have (rename=(date=char_date));
  date=input(char_date,date9.);
  format date date9.;
run;

I first rename the variable you already have - in the set statement - so no variable named date is in the data at that moment.  Now when I use the assingment date=, this creates a new variable called date - which as I have not specified a length/type, defaults to numeric 8.  Into this new variable I then put the result of the input() function, and its this new variable which is formatted.  

ballardw
Super User

You posted this code:

datel=input(date,date9.);

totday=intck('day',daterand,date); 

BY ANY chance did you try to use the DATEL(numeric) variable instead of DATE(character) in the INTCK call

 

PaulaC
Fluorite | Level 6
that was a typo. my input statement code was: date=input(date,date9.); but I got an error message when used format date date9. When I changed the input to daten=input(date,date9.) and format daten date9. then the error message disappeared and a new variable daten was created.
ballardw
Super User

@PaulaC wrote:
that was a typo. my input statement code was: date=input(date,date9.); but I got an error message when used format date date9. When I changed the input to daten=input(date,date9.) and format daten date9. then the error message disappeared and a new variable daten was created.

To minimize typo issues, and especially when you run code with an error, go to the SAS log and copy the code with the error and diagnostic messages. Then paste into a code box using the {i} icon. The code box will retain the formatting of the orginal log and the error message often has an _ indicating the exact place SAS encountered the trigger for the message. The forum main text boxes actually reformat your pasted text and loses information like where SAS placed that underscore.

PaulaC
Fluorite | Level 6
ok. thank you for the tip. I did not realize that.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 33663 views
  • 4 likes
  • 3 in conversation