Reading raw data from a text file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Reading raw data from a text file

Hi - I'm trying to read in a raw text file (.f03), that isn't delimited consistently throughout. I'm able to read it in using a proc import dbms=csv option, and it puts everything into one column. I essentially want to be able to pull 2 different statistics out of this entire file. Is there a way to scan the one variable to keep the stat that comes after "ESTIMATED PROPORTION CONSISTENTLY CLASSIFIED ="? This only appears once in the file.

 

image.png


Accepted Solutions
Solution
‎05-17-2018 03:20 PM
Respected Advisor
Posts: 3,054

Re: Reading raw data from a text file

data try;
    set testing;
    if find(var1,'estimated proportion consistently classified','i')>0 then do;
        value=scan(var1,-1)+0;
        output;
    end;
run;
--
Paige Miller

View solution in original post


All Replies
Respected Advisor
Posts: 3,054

Re: Reading raw data from a text file

Sure.

 

In a data step, use the FIND function to see if the line has ESTIMATED PROPORTION CONSISTENTLY CLASSIFIED

 

If so, then use the SCAN function to retrieve the last "word" which in this case is .875, convert character to numeric, then output to the data set.

--
Paige Miller
Occasional Contributor
Posts: 10

Re: Reading raw data from a text file

Posted in reply to PaigeMiller

Thank you! Is there anyway you can give me a syntax example of what you're thinking? I'm having trouble getting the scan function to work without explicitly stating what that statistic is.

Respected Advisor
Posts: 3,054

Re: Reading raw data from a text file


@NR13 wrote:

Thank you! Is there anyway you can give me a syntax example of what you're thinking? I'm having trouble getting the scan function to work without explicitly stating what that statistic is.


If the line of text has been found to have the desired text string of ESTIMATED PROPORTION CONSISTENTLY CLASSIFIED, then you can use the SCAN function like this (assuming we are not very creative and the text string is a variable named TEXTSTRING)

 

number = scan(textstring,-1);
--
Paige Miller
Occasional Contributor
Posts: 10

Re: Reading raw data from a text file

Posted in reply to PaigeMiller

I'm sorry - I know i'm probably doing something stupid. This is what I'm trying:

 

Data Try;
Set Testing;
Correct=Scan(find(Var1,"ESTIMATED PROPORTION CONSISTENTLY CLASSIFIED"),-1);
Run;

Solution
‎05-17-2018 03:20 PM
Respected Advisor
Posts: 3,054

Re: Reading raw data from a text file

data try;
    set testing;
    if find(var1,'estimated proportion consistently classified','i')>0 then do;
        value=scan(var1,-1)+0;
        output;
    end;
run;
--
Paige Miller
Occasional Contributor
Posts: 10

Re: Reading raw data from a text file

Posted in reply to PaigeMiller

You're the best - thank you!!

Super User
Posts: 13,582

Re: Reading raw data from a text file

You can also use a column "pointer" based on the value of text. If the key text is not found then nothing would be read. Keep only the records with values. A raw example;

data junk;
   infile datalines missover;
   input @"Some text" x;
datalines;
Some text 123
No key text 456
893 Some text 333
;
run;

And to keep only records with the value:

 

data junk;
   infile datalines missover;
   input @"Some text" x;
   if x ne .;
datalines;
Some text 123
No key text 456
893 Some text 333
;
run;
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 170 views
  • 1 like
  • 3 in conversation