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.
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
;
How elegant!
Thank you!
If it's not too much trouble, would you mind describing the _INFILE_ line in the code...?
Anca.
: 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
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.
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?
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
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
;
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
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
;
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.
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.