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

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!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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;

image.png

 

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

So your issue is that part of the day variable is somehow included in the year variable?

 

Can you attach the csv file?

 

 

Cynthia_sas
SAS Super FREQ

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

Kiko
Fluorite | Level 6

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!

monthdayyear
1022016
1022016
1032016
11032016
1152016
2112016
3082016
1012016
3082016
2192016
art297
Opal | Level 21

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

 

PeterClemmensen
Tourmaline | Level 20

Are these numeric or character variables?

Kiko
Fluorite | Level 6

They are numeric!

PeterClemmensen
Tourmaline | Level 20

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 🙂

Tom
Super User Tom
Super User

@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;

image.png

 

azharuddin
Calcite | Level 5

Thanks for providing the answer on importing data into SAS.

 

https://mindmajix.com/sas-training

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