BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AncaTilea
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

: 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

11 REPLIES 11
art297
Opal | Level 21

: 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

;

AncaTilea
Pyrite | Level 9

How elegant!

Thank you!

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

Anca.

art297
Opal | Level 21

: 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

AncaTilea
Pyrite | Level 9

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.

data_null__
Jade | Level 19

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?

art297
Opal | Level 21

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

lekha
Calcite | Level 5

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

    ;

AncaTilea
Pyrite | Level 9

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.

PGStats
Opal | Level 21

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
AncaTilea
Pyrite | Level 9

Very nice,

This code I can understand.

Smiley Happy

I would give you Correct Answer.

Anca.

Haikuo
Onyx | Level 15

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

;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 11 replies
  • 1163 views
  • 10 likes
  • 6 in conversation