Read in data

Accepted Solution Solved
Reply
Super Contributor
Posts: 543
Accepted Solution

Read in data

Hi.

How can I modify this snippet of code such that it reads the name as "John Doe"?

data foo;

    input name $ age score;

datalines;

John Doe 23 25

Mike 23 26

Meow 1 4

    ;

Thanks.

Anca.


Accepted Solutions
Solution
‎10-17-2013 07:56 PM
PROC Star
Posts: 7,492

Re: Read in data

Posted in reply to AncaTilea

: here is one possibility:

data want;

  informat name $50.;

  input @;

  _infile_=substr(_infile_,1,anydigit(_infile_)-1)||"  "||substr(_infile_,anydigit(_infile_));

  input name & age score;

  datalines;

John Doe 23 25

Mike 23 26

Meow 1 4

;

View solution in original post


All Replies
Solution
‎10-17-2013 07:56 PM
PROC Star
Posts: 7,492

Re: Read in data

Posted in reply to AncaTilea

: here is one possibility:

data want;

  informat name $50.;

  input @;

  _infile_=substr(_infile_,1,anydigit(_infile_)-1)||"  "||substr(_infile_,anydigit(_infile_));

  input name & age score;

  datalines;

John Doe 23 25

Mike 23 26

Meow 1 4

;

Super Contributor
Posts: 543

Re: Read in data

How elegant!

Thank you!

If it's not too much trouble, would you mind describing the _INFILE_ line in the code...?

Anca.

PROC Star
Posts: 7,492

Re: Read in data

Posted in reply to AncaTilea

: Feel free to give pgstats the correct answer .. I've already got far more points than I know what to do with.  However, here is the answer to your question.

_infile_, as I understand it, is an automatic system variable that is created whenever one uses an input statement and, if one continues to read the same record, the _infile_ variable is what is read, rather than the actual data record.

As such, using an input @ statement, without inputting any specific variable(s), one can modify the _infile_ variable and, upon using another input statement, they are actually applying it to the _infile_ variable.  Thus, what my suggested code was doing, was simply adding an extra space before the first number that existed in the record.  That way, your data could be read as if it had been properly formatted, i.e., with at least two spaces before a variable if the & modifier were used.

Art

Super Contributor
Posts: 543

Re: Read in data

Thank you again!

Unrelated to this topic, I would like to THANK YOU for your (inspiring) presentation at MWSUG at the Expert Panel discussion (the bank robberies problem), I was very much looking forward to learn from the experts how did they tackle the problem, and with the exception of Art C, I was truly disappointed in the solution given: this data is too dirty!

And you made all that go away by showing the most elegant (possible) solution!

So I thank you!

Anca.

Respected Advisor
Posts: 3,799

Re: Read in data

Posted in reply to AncaTilea

Anca tilea wrote:

(inspiring) presentation at MWSUG at the Expert Panel discussion (the bank robberies problem),

Is there a record of this most singular event?

PROC Star
Posts: 7,492

Re: Read in data

Posted in reply to data_null__

and DN: MWSUG was supposed to publish a summary of the discussion, but I haven't seen anything as yet.  They did post the question and data at: MidWest SAS Users Group - Experts Panel

My paper and powerpoint can be found at: Expert Panel Solution MWSUG 2013-Tabachneck - sasCommunity

New Contributor
Posts: 3

Re: Read in data

Posted in reply to AncaTilea

This is the code if you want to read the name (first and last) into one variable

data foo;

    input name $ 1-8 age score;

datalines;

John Doe 23 25

Mike       23 26

Meow       1   4

    ;

Super Contributor
Posts: 543

Re: Read in data

Lekha,

You have modified the way the data was given.

I needed for that specific data records,you aligned the age and score so that name takes up to 8 characters.

Thanks by had the solution I was hoping for.

Cheers!

Anca.

Respected Advisor
Posts: 4,934

Re: Read in data

Posted in reply to AncaTilea

An alternative would be :

data foo;

length name $24;

input @;

nLen = anydigit(_infile_) - 1;

input name $varying24. nLen age score;

drop nLen;

datalines;

John Doe 23 25

Mike 23 26

Meow 1 4

;

PG

PG
Super Contributor
Posts: 543

Re: Read in data

Very nice,

This code I can understand.

Smiley Happy

I would give you Correct Answer.

Anca.

Respected Advisor
Posts: 3,156

Re: Read in data

Here to save Drop statement,

data foo;

length name $24;

input @;

_n_ = anydigit(_infile_) - 1;

input name $varying24. _n_ age score;

datalines;

John Doe 23 25

Mike 23 26

Meow 1 4

;

🔒 This topic is solved and locked.

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

Discussion stats
  • 11 replies
  • 513 views
  • 10 likes
  • 6 in conversation