Hello
I am trying to import a csv file into SAS. One of the variables DOB was created using the following variables: 1) month 2) day 3) year.
When I read it in SAS I used the following format: Month (.2) Day (.2) Year (.4). DOB (date9.). When it's done I noticed that DOB was calculated incorrectly so I went back to the csv file and realized the issue was with the csv file. For instance,
Month Day Year DOB
1 0 22016 . (should be 1/2/2016)
1 0 62016 . (should be 1/6/2016)
4 2 42016 . (should be 4/24/2016)
12 1 32016 . (should be 12/13/2016)
10 2 2016 10/2/2016 (how do I know if it is 10/2/2016 or 10/20/2016)
All I know is that the original data was a txt file and converted into csv by someone else. Unfortunately, I do not know how he/she did it also do not have access to the original text file. Can anyone help me how to resolve the issue here?
Thank you!
@Kiko wrote:
Hi This is what the csv file looks like. Come to think of it that 10/1/2016 must have been 10/10/2016 otherwise I would have ended up with 10/0/12016 instead. Any comments would be much appreciated!
month day year 1 0 22016 1 0 22016 1 0 32016 11 0 32016 1 1 52016 2 1 12016 3 0 82016 10 1 2016 3 0 82016 2 1 92016
CSV files normally don't look like that. They don't have lines drawn around the values like they were in spreadsheet or something. Instead they have commas between the values, hence the name, comma-separated values. A CSV version of that might look like this instead:
month,day,year 1,0,22016 1,0,22016 1,0,32016 11,0,32016 1,1,52016 2,1,12016 3,0,82016 10,1,2016 3,0,82016 2,1,92016
But taking your example data you can try to convert it back to a string of digits and then try to interpret those digits as representing a date in month day year order. This version works for the data you posted.
data want ;
set have ;
dt=input(cats(month,day,year),mmddyy10.);
format dt mmddyy10..;
run;
But if your dataset that you posted has the YEAR variable as a number then it might have lost the leading zero in the YEAR value that was in the data. For example the eight value might have been like this instead in the source file.
10,1,02016
But by being read as number instead of as a string the first digit in the YEAR value was lost. You could try adding it back by using the Z format.
data want ;
set have ;
dt1=input(cats(month,day,year),mmddyy10.);
dt2=input(cats(month,day,put(year,z5.)),mmddyy10.);
same = dt1 = dt2 ;
format dt: mmddyy10.;
run;
So your issue is that part of the day variable is somehow included in the year variable?
Can you attach the csv file?
Hi:
This would be easier to figure out if we could see the actual CSV file. For example, does the CSV file actually have this:
1,0,22016
1,0,62016
OR does it have this:
1,02,2016
1,06,2016
It could be a problem with the program you used to read the CSV file. But without the data or the program you used, it is hard to do more than guess.
cynthia
Hi This is what the csv file looks like. Come to think of it that 10/1/2016 must have been 10/10/2016 otherwise I would have ended up with 10/0/12016 instead. Any comments would be much appreciated!
month | day | year |
1 | 0 | 22016 |
1 | 0 | 22016 |
1 | 0 | 32016 |
11 | 0 | 32016 |
1 | 1 | 52016 |
2 | 1 | 12016 |
3 | 0 | 82016 |
10 | 1 | 2016 |
3 | 0 | 82016 |
2 | 1 | 92016 |
Here is one way you could correct the data:
data have; input month day year; cards; 1 0 22016 1 0 62016 4 2 42016 12 1 32016 10 2 2016 ; data want; set have; format dob date9.; if year gt 2018 then do; if day eq 0 then do; day=input(substr(put(year,5.),1,1),8.); year=input(substr(put(year,5.),2),8.); end; else do; day=input(catt(day,substr(put(year,5.),1,1)),8.); year=input(substr(put(year,5.),2),8.); end; end; dob=mdy(month,day,year); run;
Art, CEO, AnalystFinder.com
Are these numeric or character variables?
They are numeric!
data have;
input month$ day$ year$;
datalines;
1 0 22016
1 0 22016
1 0 32016
11 0 32016
1 1 52016
2 1 12016
3 0 82016
10 1 2016
3 0 82016
2 1 92016
;
data inter;
set have;
if length(year)=5 then do;
day=cats(day, substr(year, 1, 1));
year=substr(year, length(year)-3, 4);
end;
run;
.. And convert them to numeric 🙂
@Kiko wrote:
Hi This is what the csv file looks like. Come to think of it that 10/1/2016 must have been 10/10/2016 otherwise I would have ended up with 10/0/12016 instead. Any comments would be much appreciated!
month day year 1 0 22016 1 0 22016 1 0 32016 11 0 32016 1 1 52016 2 1 12016 3 0 82016 10 1 2016 3 0 82016 2 1 92016
CSV files normally don't look like that. They don't have lines drawn around the values like they were in spreadsheet or something. Instead they have commas between the values, hence the name, comma-separated values. A CSV version of that might look like this instead:
month,day,year 1,0,22016 1,0,22016 1,0,32016 11,0,32016 1,1,52016 2,1,12016 3,0,82016 10,1,2016 3,0,82016 2,1,92016
But taking your example data you can try to convert it back to a string of digits and then try to interpret those digits as representing a date in month day year order. This version works for the data you posted.
data want ;
set have ;
dt=input(cats(month,day,year),mmddyy10.);
format dt mmddyy10..;
run;
But if your dataset that you posted has the YEAR variable as a number then it might have lost the leading zero in the YEAR value that was in the data. For example the eight value might have been like this instead in the source file.
10,1,02016
But by being read as number instead of as a string the first digit in the YEAR value was lost. You could try adding it back by using the Z format.
data want ;
set have ;
dt1=input(cats(month,day,year),mmddyy10.);
dt2=input(cats(month,day,put(year,z5.)),mmddyy10.);
same = dt1 = dt2 ;
format dt: mmddyy10.;
run;
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 25. 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.