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

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.

Screenshot (263).png

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 !

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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:

  1. Lead off the household or member variable input statements with "@1".
  2. End the single character read by positioning the pointer at column one, and then ending with the trailing "@".
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

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:

  1. Lead off the household or member variable input statements with "@1".
  2. End the single character read by positioning the pointer at column one, and then ending with the trailing "@".
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
CarmineVerrell
SAS Employee

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;

lol123
Fluorite | Level 6
@carmine
Hey , thank you for your suggestion !
I still do not know some of the functions yet, but I will try your suggestion and figure it out !
Thank you once again 🙂
lol123
Fluorite | Level 6
@mkeintz
Hey, thank you so much for your suggestion !!!
I had been trying to use the whole variable in the "if" statement and never thought of using only one character in the variable.
Thank you 🙂
Astounding
PROC Star

@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
;
lol123
Fluorite | Level 6
@Astounding
Hey, thank you for your additional suggestion!!!
Yes, the task is difficult to me since I only started to learn SAS few weeks ago.
I will figure out your suggestion !
Thank you 😄

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 1320 views
  • 4 likes
  • 4 in conversation