n Thu, 9 Sep 2004 15:57:18 -0400, Miller, Jeremy T. < XXXX@XXXXX.COM > wrote:
I assume that your input data, dat1, is a sas dataset with two variables,
id and line.
If so, the following will do. The code depends on many other assumptions.
Some of them are:
(1) there are always two blank lines before the question number 1.
(2) a line with "Sent" signals the end of one id's responses.
(3) question number line is always followed by an answer line, which, in
turn, is followed by a blank line.
(4) if the answer is "Other" then it is followed by:
a blank line, a question number line with the same question number,
then a line of comment (only a single line up to 100 chars.), then
finally a blank line.
The sas code uses link, which enables us to get an observation from many
places in the code. if you use multiple set statements outside of link,
then you will end up doing the self-interleaving.
The set statement is wrapped in an always false if-then-do block, in order
to prevent to be executed each time the data step loops. I first saw this
trick mentioned by Dorfman. It is used here to let the sas compiler know
that the variable, line, is of a character type with a length of 100
chars. Then, I cheat with specifying "length id 8" before the block --
just to get the variable order right. ... :-)
infile cards truncover;
length id 8 line $100;
id = 1;
input line $char100.;
Sent: Thursday, March 21, 2002 8:28 PM
Subject: Survey-Hepatitis B and You
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
google search engine search
can someone just get any hepatitis virus without ....
length id 8 ques 8 answr $5 comment $100;
if 0 then do; /* a hack by Dorfman */
if line="Sent" then stop;
if _n_ = 1 then do until (blanks=2);
if missing(line) then blanks + 1;
ques = input(substr(line,1,1), best.);
answr = substr(line,1,5);
if answr = "Other" then do;
if ques ^= input(substr(line,1,1), best.) then do;
put "ERROR: the same question number expected.";
end; else do;
comment = line;
keep id Ques Answr Comment;
proc print data=dat2 noobs;
/* on lst
id ques answr comment
1 1 No
1 2 No
1 3 Other google search engine search
1 4 Yes
1 5 Yes
1 6 Other can someone just get any hepatitis virus
1 8 Yes