BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bimohkc
Calcite | Level 5
Data summary;
infile 'D:\SASdatafile\survey.txt' dlm=',';
input Household_ID: $10. Type $ no_of_members @;
no_of_males=0;
no_of_females=0;
no_of_employed=0;
household_income=0;
do index=1 to no_of_members;
input Household_Mem_No H_indicator$ DOB: date9. Gender $ M_Status $ E_level E_status $ M_income @;
if Gender ='M' then no_of_males +1;
else no_of_females +1;
if E_status = 'FT' or E_status = 'PT' then no_of_employed+1;
if H_indicator='Y' then Age_Householder=yrdif(dob,'30dec2008'd,'Actual');
Age_Householder=round(Age_Householder,0.5);
household_income +M_income;
end;
average_income=Household_income/no_of_members;
average_income=round(average_income,0.01);
keep Household_ID type no_of_males no_of_females no_of_employed Age_Householder average_income ;
run;

The data of survey.txt is as follows:

A123456789,A,3
1,Y,15Feb1960,M,M,3,FT,55000
2,N,3Jun1965,F,M,3,UE,0
3,N,24Jan1988,M,S,2,NA,3000
A135790234,B,1
1,Y,19Oct1944,F,D,0,NA,3000
B234523456,A,2
1,Y,30Jun1978,F,M,1,PT,4000
2,N,21May1975,M,M,2,FT,30000
1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13
It will write each time you read. Try it and see if that is what you want.

View solution in original post

7 REPLIES 7
WarrenKuhfeld
Ammonite | Level 13

Not sure precisely what you are asking, but the structure of your code suggests to me that you might want an OUTPUT statement before your END statement in your DO loop.

bimohkc
Calcite | Level 5
What will be the effect of adding an OUTPUT statement? Is it possible to output all the detail records?
WarrenKuhfeld
Ammonite | Level 13
It will write each time you read. Try it and see if that is what you want.
bimohkc
Calcite | Level 5
It works. But can you briefly explain how the record will be changed in each iteration of the first record?thanks a lot.
WarrenKuhfeld
Ammonite | Level 13

Here is a sketch of a typical data step;

 

data ...;

infile ...;

input ...;

run;

 

Looping is implicit and there is an implicit execution of an OUTPUT before the run (one for each input obs read).

 

You did this, which is perfectly valid.

data ....;

infile ....;

do ....;

input ...;

end;

run;

 

Read many write one.  There is still one implicit output.


Since you said that something was missing, I thought you might want.

 

data ....;

infile ....;

do ....;

input ...;

output;

end;

run;

write each time you read by using an explicit OUTPUT;

Tom
Super User Tom
Super User

Why there is only one obs per header record output to the data set instead of outputting all obs?

Because you read the entire household in one pass of the data step.

Since you are only keeping household level variables why would you want it to output more than one observation per household?

 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1260 views
  • 0 likes
  • 3 in conversation