Hi guys,
I am a beginner of SAS and I need some urgent helps.
Based on this question, I need to create a SAS data set but I do not know how to proceed with the If-else statement.
This data set consists of a header record for a household and immediately followed by one record for each household member if applicable. If the household doesn't consist of any members, the observation must not be included in the data set.
What should I do in the if-else statement for the program to read the household and its corresponding members into one line of observation? How to ensure that the observation for household without members would be skipped ?
It seems stupid but this is my code and I could not create the data set 😞
data Members;
infile datalines dlm=',';
input houseid :$15. @;
if houseid not in ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC') then do;
input type_house: $1. ;
end;
else if houseid in ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC') then do;
input birthdate: date11. hindicator$ gender$ marital$ education employment$ income;
end;
retain house_id type_house;
datalines;
A1234567BC012,A
15/FEB/1980,Y,Male,Married,3,FT,55000
3/JUN/1982,N,Female,Married,3,UE,0
24/JAN/2005,N,Male,Unknown,2,NA,0
D135EG023456789,B
19/OCT/1950,Y,Female,Divorced,0,PT,5000
X123A567F9,A
B2345234CC,A
21/MAY/1975,N,Male,Married,2,FT,30000
30/JUN/1978,Y,Female,Married,1,PT,10000
;
run;
Your helps are much appreciated. Thank you !
Apparently all the household id's begin with an uppercase letter. If so, then just read the first character, making sure to have a trailing @ as you do now, in order to prepare for reading of either household or member data.
If it is between 'A' and 'Z' then input household variables. Otherwise do TWO things: (1) read the member variable, and (2) explicitly output the member record.
One caveat. After reading the first character, you should make sure the column pointer is set to column 1 either before or during reading in the household or member variables. That can be done in a couple of ways:
Apparently all the household id's begin with an uppercase letter. If so, then just read the first character, making sure to have a trailing @ as you do now, in order to prepare for reading of either household or member data.
If it is between 'A' and 'Z' then input household variables. Otherwise do TWO things: (1) read the member variable, and (2) explicitly output the member record.
One caveat. After reading the first character, you should make sure the column pointer is set to column 1 either before or during reading in the household or member variables. That can be done in a couple of ways:
Hello, as pointed out you need to use the trailing @ sign to be able to accomplish this task at the end of the input. Here is an example below of a possible solution.
Hope this helps.
Carmine
data Members(drop=frst15chars type_house getmonth);
Length Lhouseid $15 TypeH $1 ;
Retain Lhouseid TypeH;
infile datalines dlm=',' ;
/* only execute if there has not been a house id identified*/
if lhouseid=" " then do; input frst15chars :$15. @ ;
Lhouseid=frst15chars;
input type_house: $1. ;
TypeH=type_house;
return;
end;
input frst15chars :$15. @;
GetMonth=upcase(strip(substr(frst15chars, find(frst15chars,'/')+1,3)));
if find("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC",strip(GetMonth),'i') then do;
birthdate=input(frst15chars,ANYDTDTE11.);
input hindicator$ gender$ marital$ education employment$ income ;
output;
end;
else
do;
Lhouseid=frst15chars;
input type_house: $1.;
TypeH=type_house;
return;
end;
datalines;
A1234567BC012,A
15/FEB/1980,Y,Male,Married,3,FT,55000
3/JUN/1982,N,Female,Married,3,UE,0
24/JAN/2005,N,Male,Unknown,2,NA,0
D135EG023456789,B
19/OCT/1950,Y,Female,Divorced,0,PT,5000
X123A567F9,A
B2345234CC,A
21/MAY/1975,N,Male,Married,2,FT,30000
30/JUN/1978,Y,Female,Married,1,PT,10000
;
run;
@mkeintz mentioned exactly the right set of issues. However, it's probably a daunting task if you are a beginner in SAS. So here's something that incorporates his suggestions.
data Members;
infile datalines dlm=',';
input first_char :$1. @1 @;
if ('A' <= upcase(first_char) <= 'Z') then input houseid : $15. type_house : $1.;
else do;
input birthdate: date11. hindicator$ gender$ marital$ education
employment$ income;
output;
end;
retain house_id type_house;
datalines;
A1234567BC012,A
15/FEB/1980,Y,Male,Married,3,FT,55000
3/JUN/1982,N,Female,Married,3,UE,0
24/JAN/2005,N,Male,Unknown,2,NA,0
D135EG023456789,B
19/OCT/1950,Y,Female,Divorced,0,PT,5000
X123A567F9,A
B2345234CC,A
21/MAY/1975,N,Male,Married,2,FT,30000
30/JUN/1978,Y,Female,Married,1,PT,10000
;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.