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!
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;
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.
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.
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;
This worked! Thanks!
Use an if condition
if not missing(variable) then new_var = ...
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: