Help using Base SAS procedures

reading a portion of a textfile

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

reading a portion of a textfile

hello SAS experts,

I have about 120 textfiles that I need read a portion of it, an example of the file is attached. My code does not work and need help find the problem.

Thank you.

My Code:

data it.temp_1;                                                                                                                  

infile "C:\CDM\result\CDM_1.out" truncover;                                                                                               

input test 9. @;

if test="BASED ON ESTIMATED POSTERIOR PROBABILITIES" then do;                                                                                                

input  /  @3 class                                                                                                             

          @17 popul                                                                                                                 

          @37 proportion;                                                                                                       

output;                                                                                                                               

stop;                                                                                                                                 

end;                                                                                                                                   

drop test;                                                                                                                             

run; 

The portion I need to read:

FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASS PATTERNS

BASED ON ESTIMATED POSTERIOR PROBABILITIES

     Latent

   Classes

       1       3689.92934          0.36899

       2        468.37301          0.04684

       3        269.98721          0.02700

       4        309.98844          0.03100

       5        685.71226          0.06857

       6        428.21773          0.04282

       7        528.04662          0.05280

       8       3619.74539          0.36197

Attachment

Accepted Solutions
Solution
‎10-15-2011 11:19 PM
PROC Star
Posts: 7,468

reading a portion of a textfile

I think that the following will achieve what you want:

data it.temp_1;

  infile "C:\CDM\result\CDM_1.out"  truncover;

  input;

  if index(upcase(_infile_),

   "BASED ON ESTIMATED POSTERIOR PROBABILITIES")

   then do;

    input;input;input;input;

    do until (missing(class));

      input class popul proportion;

      if not missing(class) then output;

    end;

  end;

run;

View solution in original post


All Replies
Solution
‎10-15-2011 11:19 PM
PROC Star
Posts: 7,468

reading a portion of a textfile

I think that the following will achieve what you want:

data it.temp_1;

  infile "C:\CDM\result\CDM_1.out"  truncover;

  input;

  if index(upcase(_infile_),

   "BASED ON ESTIMATED POSTERIOR PROBABILITIES")

   then do;

    input;input;input;input;

    do until (missing(class));

      input class popul proportion;

      if not missing(class) then output;

    end;

  end;

run;

Regular Contributor
Posts: 161

reading a portion of a textfile

Thank you art297, it does work well, but may I ask what do these codes do:

1) if index(upcase(_infile_),

2) input;input;input;input; (isn't only 3 variables ? then why 4 inputs?)

3)    do until (missing(class));

Thanks again

PROC Star
Posts: 7,468

reading a portion of a textfile

Sure you can ask:

1) if index is simply using the index function to see if a text string exists in a variable

2) the four input statements simply skip four records

3) do until (missing(class)) .. as I understood your file, and request, you only wanted the data that existed from the time a record contained the string "BASED ON ESTIMATED POSTERIOR PROBABILITIES", then needed to skip four records, then, finally read all of the remaining records until a value for class wasn't found.  That is what this statement was intended to accomplish.

Regular Contributor
Posts: 161

reading a portion of a textfile

thank you very much. This helps plenty

RG

Valued Guide
Posts: 765

Re: reading a portion of a textfile

Hi ... if the files all have this structure, here's another idea ...

data x;

infile 'z:\cdm_1.out';

input @ "FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASS PATTERNS" ////;

do _n_=1 to 8;

   input class popul proportion;

   output;

end;

stop;

run;

Regular Contributor
Posts: 161

Re: reading a portion of a textfile

Thanks for the code, the code is definitely simpler but unfortunately doesn't work on my file, I tried it for one of the files but doesn't read the numbers, I do not know why.

thanks anyway

R

PROC Star
Posts: 7,468

Re: reading a portion of a textfile

Did you change the directory letter which Mike had as "z:\"?  His code worked on my system.

Valued Guide
Posts: 765

Re: reading a portion of a textfile

Hi ... in addition to making sure that the FOLDER is correct (as Art pointefd out), the SPACING and CASE of the text in the INPUT statement must match that in the data file.  Did you try just cut/paste/run the posted code?  Like Art said, it does work with the file you posted.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 190 views
  • 3 likes
  • 3 in conversation