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-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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