BookmarkSubscribeRSS Feed
InfoAlisaA
Calcite | Level 5

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!!

4 REPLIES 4
art297
Opal | Level 21

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;

InfoAlisaA
Calcite | Level 5

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

Tom
Super User Tom
Super User

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;


ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1072 views
  • 0 likes
  • 4 in conversation