DATA Step, Macro, Functions and more

Converting ddmmmyyyy character date to date9.

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Converting ddmmmyyyy character date to date9.

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


Accepted Solutions
Solution
‎02-10-2017 04:40 PM
Super User
Super User
Posts: 7,401

Re: Converting ddmmmyyyy character date to date9.

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


All Replies
Super User
Super User
Posts: 7,401

Re: Converting ddmmmyyyy character date to date9.

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.

Contributor
Posts: 43

Re: Converting ddmmmyyyy character date to date9.

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.
Super User
Super User
Posts: 7,401

Re: Converting ddmmmyyyy character date to date9.

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

Contributor
Posts: 43

Re: Converting ddmmmyyyy character date to date9.

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.
Solution
‎02-10-2017 04:40 PM
Super User
Super User
Posts: 7,401

Re: Converting ddmmmyyyy character date to date9.

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.  

Super User
Posts: 10,500

Re: Converting ddmmmyyyy character date to date9.

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

 

Contributor
Posts: 43

Re: Converting ddmmmyyyy character date to date9.

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.
Super User
Posts: 10,500

Re: Converting ddmmmyyyy character date to date9.


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.

Contributor
Posts: 43

Re: Converting ddmmmyyyy character date to date9.

ok. thank you for the tip. I did not realize that.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 1954 views
  • 4 likes
  • 3 in conversation