Help using Base SAS procedures

Stuck on an Infile

Reply
Frequent Contributor
Posts: 90

Stuck on an Infile

Hello Everyone,

This is the raw data file that I am trying to infile:

J. Mitchell         Very Well done!  Rating:5

Amy Jung            Rating:4

Carl Heisman           Rating:4

Linda Deal          Not enough give aways  Rating:3

Gabrielle Heron     Nice! Rating:4

                    Not helpful at all Rating:2

Kyle Patterson      Very good. Need more like it  Rating:5

As you can see, there are a lot of variables that have different lengths.

This is the program that I have so far:

data seminar_ratings;

  infile 'G:\hw_data\seminar.dat';

  input Name & $ 1-17 @19 Comment & $28.  @'Rating:' Rating;

run;

proc print data=seminar_ratings;

run;

The issue that I am having is that the entries that have no comments are having the Ratings read in as comments.

I am really stuck as to what I should do so I have blank comments instead of the Ratings.

Could someone look at my code and let me know what I should do in this situation?

Thanks!!

PROC Star
Posts: 7,365

Re: Stuck on an Infile

You can correct it either when inputting or after the fact.  One way to do it after the fact might be:

data seminar_ratings;

  infile 'G:\hw_data\seminar.dat';

  input Name & $ 1-17 @19 Comment & $28.  @'Rating:' Rating;

  if index(comment,'Rating:') gt 0 then do;

    if index(comment,'Rating:') gt 2 then do;

      comment=substr(comment,1,index(comment,'Rating:')-1);

    end;

    else call missing(comment);

  end;

run;

Frequent Contributor
Posts: 90

Re: Stuck on an Infile

Hello Arthur,

Thank you for the feedback on this.

Unfortunately, I have to do this without substr and indexes.

If you have another suggestion, that would be great!

Thanks,

Alisa

Super User
Super User
Posts: 6,502

Re: Stuck on an Infile

Is it a homework assignment?  Otherwise use the method that works and is clear.

If you can contact the person that created the file ask them to switch the location of the rating and the comment so that the variable information is at the end of the line.

J. Mitchell         Very Well done!  Rating:5

Amy Jung            Rating:4

Carl Heisman           Rating:4

Linda Deal          Not enough give aways  Rating:3

Gabrielle Heron     Nice! Rating:4

                    Not helpful at all Rating:2

Kyle Patterson      Very good. Need more like it  Rating:5


data want ;

  infile 'xxx' truncover ;

  input name $17. @;

  length rating 8 comment $100 word $100 ;

  do until (word in: (' ' , 'Rating:') ) ;

    input word @ ;

    if word not in: (' ', 'Rating:') then comment=catx(' ',comment,word);

  end;

  input @1 @'Rating:' rating;

  drop word;

run;


Super User
Posts: 10,549

Re: Stuck on an Infile

What is the data source file type? It may be that you can use or create a delimiter when saving the data such as in a CSV format which will allow better read control.

Ask a Question
Discussion stats
  • 4 replies
  • 320 views
  • 0 likes
  • 4 in conversation