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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: