note : raw data line are actually different twitter comments. Need to fetch only those data which has special character (with character) as shown in output and those comment does not contains special character then make it null.
Raw data
@nice#Greatwork@fake
very nice portal ncs
Good portal @hatsoff
output needed :
@nice
#Greatwork
@fake
null
@hatsoff
A not to complex regular expression and call prxnext can solve the problem:
data work.have;
length lot $ 100;
input;
lot = trim(_infile_);
datalines;
@nice#Greatwork@fake
very nice portal ncs
Good portal @hatsoff
;
run;
data work.want;
set work.have;
length
word $ 100
rx start p l 8
;
retain rx;
if _n_ = 1 then do;
rx= prxparse('/([@#]\w+)/i');
end;
start = 1;
call prxnext(rx, start, -1, lot, p, l);
do while (p > 0);
word = substr(lot, p, l);
output;
call prxnext(rx, start, -1, lot, p, l);
end;
if missing(word) then do;
word = 'null';
output;
end;
keep word;
run;
Look at the SCAN() function including the third parameter, which allows you specify the delimiter.
Here's a fully worked example. Specify your delimiters as needed, make sure to include spaces in there if required.
http://blogs.sas.com/content/iml/2016/07/11/break-sentence-into-words-sas.html
I don't think these will give you a NULL value, but if you really need it add in a check if the number of words is 0 then output a null record.
A not to complex regular expression and call prxnext can solve the problem:
data work.have;
length lot $ 100;
input;
lot = trim(_infile_);
datalines;
@nice#Greatwork@fake
very nice portal ncs
Good portal @hatsoff
;
run;
data work.want;
set work.have;
length
word $ 100
rx start p l 8
;
retain rx;
if _n_ = 1 then do;
rx= prxparse('/([@#]\w+)/i');
end;
start = 1;
call prxnext(rx, start, -1, lot, p, l);
do while (p > 0);
word = substr(lot, p, l);
output;
call prxnext(rx, start, -1, lot, p, l);
end;
if missing(word) then do;
word = 'null';
output;
end;
keep word;
run;
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.