Importing data into SAS

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Importing data into SAS

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!

 

 

 


Accepted Solutions
Solution
‎12-27-2017 05:18 PM
Super User
Super User
Posts: 7,399

Re: Importing data into SAS


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


All Replies
PROC Star
Posts: 1,075

Re: Importing data into SAS

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

 

Can you attach the csv file?

 

 

SAS Super FREQ
Posts: 9,038

Re: Importing data into SAS

[ Edited ]

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

Contributor
Posts: 22

Re: Importing data into SAS

Posted in reply to Cynthia_sas

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
PROC Star
Posts: 7,800

Re: Importing data into SAS

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

 

PROC Star
Posts: 1,075

Re: Importing data into SAS

Are these numeric or character variables?

Contributor
Posts: 22

Re: Importing data into SAS

They are numeric!

PROC Star
Posts: 1,075

Re: Importing data into SAS


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 Smiley Happy

Solution
‎12-27-2017 05:18 PM
Super User
Super User
Posts: 7,399

Re: Importing data into SAS


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

 

New Contributor
Posts: 4

Re: Importing data into SAS

[ Edited ]

Thanks for providing the answer on importing data into SAS.

 

https://mindmajix.com/sas-training

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 260 views
  • 1 like
  • 6 in conversation