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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
Reeza
Super User

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. 

epigrad123
Calcite | Level 5

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.

ballardw
Super User

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;
epigrad123
Calcite | Level 5

This worked! Thanks!

Reeza
Super User

Use an if condition

 

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

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
  • 6 replies
  • 1025 views
  • 1 like
  • 4 in conversation