Hi,
I'm trying to read values from a text file but unfortunately they're held in pretty weird format, for example the names 'Pete Smith' and 'John Brown' could be held like this;
name begin
pe
te
smi
th
end name
begin name
joh
n
br
o
wn
end name
Is it possible to read these into a dataset using a single input statement or would it need a combination of several inputs? Any advice would be appreciated.
OK, here's one way.
data want;
length name $ 30;
retain name;
length segment $ 20;
input segment &;
if segment='begin name' then name=' ';
else if segment='end name' then output;
else name = cats(name, segment);
run;
But the problem of identifying the separation point between first and last name still exists.
A DATA step could handle this except for one feature. How do you know when the first name ends and the last name begins?
Hi, yes that's a problem, I think for now I would just like to get them into a single variable.
OK, here's one way.
data want;
length name $ 30;
retain name;
length segment $ 20;
input segment &;
if segment='begin name' then name=' ';
else if segment='end name' then output;
else name = cats(name, segment);
run;
But the problem of identifying the separation point between first and last name still exists.
Thanks for that, works well with a Retain so is the best solution for now I think
data want; length name $ 30; length segment $ 20; input segment &; retain name; if segment='begin name' then name=' '; else if segment='end name' then do; drop segment; output; end; else name = cats(name, segment); datalines; begin name pe te smi th end name begin name joh n br o wn end name run;
Attach a TEXT file to let us test it.
With data like that: impossible.
Simply because you cannot state a clear rule where words are separated; one coould only read in the names as strings without delimiters.
You first need a clear rule where the given name ends and the surname begins, then you can think about code.
The code would then actually be very simple.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.