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.
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;
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;
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
;
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.