BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Attyslogin
Obsidian | Level 7

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
error_prone
Barite | Level 11

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;

View solution in original post

4 REPLIES 4
Reeza
Super User

Look at the SCAN() function including the third parameter, which allows you specify the delimiter. 

Attyslogin
Obsidian | Level 7
can you show me the exact code ?
Reeza
Super User

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.

error_prone
Barite | Level 11

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1336 views
  • 2 likes
  • 3 in conversation