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

Hi all;

 This text contains data on id, location ,date1, gender and dob, but dates in different formats. also the length of gender is different. how can i read this dataset using 'infile'?

 

id    location date1  gender DOB

 

001 pun 09-16-2010 male 04-28-1959
002 MUM 30MAY2010 F 15AUG1960
003 pun 08-18-2010 female 10-11-1961
004 MUM 12FEB2011 M 29APR1962

 

please answer the question.

thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Read into character variables, and convert:

data want;
input id :$3. location :$3. _date1 :$10. _gender $ _DOB :$10.;
format
  date1 yymmddd10.
  gender $1.
  DOB yymmddd10.
;
date1 = ifn(length(_date1) = 10,input(_date1,mmddyy10.),input(_date1,date9.));
gender = upcase(substr(_gender,1,1));
DOB = ifn(length(_DOB) = 10,input(_DOB,mmddyy10.),input(_DOB,date9.));
drop _:;
cards;
001 pun 09-16-2010 male 04-28-1959
002 MUM 30MAY2010 F 15AUG1960
003 pun 08-18-2010 female 10-11-1961
004 MUM 12FEB2011 M 29APR1962
;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Read into character variables, and convert:

data want;
input id :$3. location :$3. _date1 :$10. _gender $ _DOB :$10.;
format
  date1 yymmddd10.
  gender $1.
  DOB yymmddd10.
;
date1 = ifn(length(_date1) = 10,input(_date1,mmddyy10.),input(_date1,date9.));
gender = upcase(substr(_gender,1,1));
DOB = ifn(length(_DOB) = 10,input(_DOB,mmddyy10.),input(_DOB,date9.));
drop _:;
cards;
001 pun 09-16-2010 male 04-28-1959
002 MUM 30MAY2010 F 15AUG1960
003 pun 08-18-2010 female 10-11-1961
004 MUM 12FEB2011 M 29APR1962
;
run;
novinosrin
Tourmaline | Level 20
data have;
input id  $  location $ date1 : anydtdte21.  gender $ DOB : anydtdte21.;
format date1 dob date9.;
cards;
001 pun 09-16-2010 male 04-28-1959
002 MUM 30MAY2010 F 15AUG1960
003 pun 08-18-2010 female 10-11-1961
004 MUM 12FEB2011 M 29APR1962
;
Kurt_Bremser
Super User

I would advise against the use of the "any" informats, because of things like this:

data have;
input id  $  location $ date1 : anydtdte21.  gender $ DOB : anydtdte21.;
format date1 dob date9.;
cards;
001 pun 09-16-2010 male 04-28-1959
002 MUM 12-03-2018 M 11-03-1972
;
run;

proc print data=have noobs;
run;

Result:

id     location        date1    gender          DOB

001      pun       16SEP2010     male     28APR1959
002      MUM       12MAR2018     M        11MAR1972

Depending on local settings, the outcome of ambiguous month/day values may be unexpected. Much better to be strict.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 799 views
  • 0 likes
  • 3 in conversation