Data Test;
input name & $9. Sale ;
datalines;
Ron Bill 435
Paul Anka 657
;
Run;
In the above example, & modifier does not work. Might be because It does not encounter two consecutive blank after name variable. In that case, what other method can be used to read this data ?
Arun
if your data will come as is (no other issues, such as middle name, etc), you can try to use _infile_; Otherwise, you will need to involve more complex logic or functions.
Data Test;
input;
length name $9.;
name=catx(' ',scan(_infile_,1),scan(_infile_,2));
Sale=input(scan(_infile_,3),best.);
datalines;
Ron Bill 435
Paul Anka 657
;
Run;
Haikuo
UPdate: on my second thought, you can just do: input (firstname lastname) (:$8.) Sale;
then :
name=catx(' ',firstname,lastname);
You could try sneeking up on it. Then you don't have to now how many words in name.
Awesome!. Get to learn $varingw. informat. Thanks, Data_null_!
Haikuo
Hai,
I did not understand what Data null,s code is doing... Could you please explain what these lines are doing.
input @;
call scan(_infile_,-1,_p,_l);
_l=_p-2;
input name $varying32. _l sale;
drop _:;
datalines;
Regards
input @;
Begin reading the next line. This will set the automatic variable _INFILE_ with the content of the next line.
call scan(_infile_,-1,_p,_l);
Find the length and position of a word in a text string. _INFILE_ is the line of text, _P and _L will get the position and length. Because -1 is negative it scans from the end to the front. So -1 is the last word.
_l=_p-2;
Set _L to two positions before the beginning of the last word. This will be used in the next statement.
input name $varying32. _l sale;
Read the first _L characters of the input line into variable NAME and then read SALE from the rest of the line.
drop _:;
Do not keep the _L and _P variables. The colon is a wildcard so it matches any variable name that starts with an underscore.
datalines;
This marks the beginning of the in line data. Us old timers use CARDS; instead. If you will be reading from a file instead then remove this and insert an INFILE statement before the first INPUT statement.
Hello Arun,
U need to be use code like this to run .
Data Test,
input name & $9. sale;
dataline;
Ron Bill 435
Paul Anka 657
;
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 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.