DATA Step, Macro, Functions and more

Date variable as a character

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Date variable as a character

I have a variable called dateofretest and the actual cells look like this:

 

01/26/2015

07/08/2014

05/06/2015

 

but the variable itself has $34. format, when i need it to be in mmddyy8.

I have tried using input, subsrtn, and other formatting things but the result is always a bunch of missing cells.

How do i get this in date-format??

 

Thanks!


Accepted Solutions
Solution
‎05-05-2016 12:39 PM
Super User
Posts: 10,516

Re: Date variable as a character

Without seeing you whole code it is hard to say why it is failing. If your data is in the form you show with all the leading spaces the substr is not needed.

This creates a date valued variable from that string.

data _null_;
    dateofrevisit='                                           08/23/2015';
    doRt=input(strip(dateofrevisit),MMDDYY10.);
    put doRt mmddyy10.;
run;

View solution in original post


All Replies
Super User
Posts: 17,861

Re: Date variable as a character

input(variable, mmddyy8.);

 

If you get warnings or errors, then check your data and decide how you want to handle those cases. For example, missing data will generate an error. 

Occasional Contributor
Posts: 8

Re: Date variable as a character

I have a lot of missing data (only about 1/3 came for a revisit). How should I address that? I found the following code that worked for changing the first visit date (everyone has a value for this variable)

 

 

dopv=input(substr(strip(firstposvisit),1,10),MMDDYY10.);

 

but it doesnt work for the revisit date. I get the error:

NOTE: Invalid numeric data, dateofrevisit='                                           08/23/2015' , at line 1205 column 4.

Solution
‎05-05-2016 12:39 PM
Super User
Posts: 10,516

Re: Date variable as a character

Without seeing you whole code it is hard to say why it is failing. If your data is in the form you show with all the leading spaces the substr is not needed.

This creates a date valued variable from that string.

data _null_;
    dateofrevisit='                                           08/23/2015';
    doRt=input(strip(dateofrevisit),MMDDYY10.);
    put doRt mmddyy10.;
run;
Occasional Contributor
Posts: 8

Re: Date variable as a character

This worked! Thanks!

Super User
Posts: 17,861

Re: Date variable as a character

Use an if condition

 

if not missing(variable) then new_var = ...

Super User
Super User
Posts: 7,407

Re: Date variable as a character

So, by cells I assume you are importing an Excel file correct?  If so therein lies your problem.  I also assume your using proc import to import the file?  The problem is with that process that you are combining Excel's native lack of structure or control, with a procedure which "guesses" what you want to import which inevitably ends in a mess of data.  

Now, I would suggest fixing the problem at source (although I don't suppose you can stop using Excel), so from that Excel file, save the data to a better data transfer format - CSV.  File-SaveAS, in Excel and choose CSV as file type.  This will create a plain text file, where each data item is separated by commas, with the first line having row headers (if present).

This is a far more workable file and should help with fixing the Excel problem.  The second step is to write an import program which reads that text file in the way that you - who know that data and are not guessing - knows how it should look - you will note the impetus is on You to know the data and code the import to read it properly.  Writing the code is very simple:

data want;
  infile "c:\csvfile.csv" dlm=",";
  informat ...variables with the format to read in as...;
  format   ... variables with the format to display in dataset...;
  length   ... length of variables...;
  input     ...specify variables and $ for character...;
run;

If you use the search box on here, you will find numerous examples of reading in various files including CSV ones with examples. 

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 276 views
  • 1 like
  • 4 in conversation