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
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;
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;
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
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.
thank you very much. This helps plenty
RG
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;
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
Did you change the directory letter which Mike had as "z:\"? His code worked on my system.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.