Help using Base SAS procedures

search with text

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

search with text

Hello Everyone!

From the below dataset, I would like to search ID value if the date present in the variable "text" > today's date.

e.g.Output value should be 101 and 104

because for 101, date available is 03 Apr 2101 which is greater than today'date

                   104, same (01 Jan 2017)

data abcl;

input id text : & $200.;

cards;

101 Prime Minister has arrived in Melbourne for the final leg of his four-city tour of Australia on 01 Mar 2014  and 03 Apr 2101'

102 The 234/12 advance rfff1111eading of Singapore third quarter on 01 Dec 2012 showing no response

103 There is no symptom minister Malaria after 20 Nov 2014 meaning vaccine work properly

104 The total cost of the conversion is a nonnegative value that is usually seen on 01 Jan 2017 showing 10 people's work

;

run;


Accepted Solutions
Solution
‎11-20-2014 07:01 AM
SAS Employee
Posts: 15

Re: search with text

Hi.

This should work:

data abcl(keep=id text);

input id text : & $200.;

format date date9.;

if _N_ = 1 then

do;

  retain rId;

  rId = prxparse('/\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4}/i');

end;

start = 1;

stop = length(text);

  

call prxnext(rId, start, stop, text, position, length);

do while (position > 0);

   date = input(substr(text, position, length),date11.);

   call prxnext(rId, start, stop, text, position, length);

   if date > date() then do;

    output;

  continue;

   end;

end;

cards;

101 Prime Minister has arrived in Melbourne for the final leg of his four-city tour of Australia on 01 Mar 2014 and 03 Apr 2101'

102 The 234/12 advance rfff1111eading of Singapore third quarter on 01 Dec 2012 showing no response

103 There is no symptom minister Malaria after 20 Nov 2014 meaning vaccine work properly

104 The total cost of the conversion is a nonnegative value that is usually seen on 01 Jan 2017 showing 10 people's work

;

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: search with text

Hi,


Well the main bit is finding the date in the text.  In the below I use an array to find the month (note the spaces around it so I don't mistake other text), then its quite simple.  I leave each bit as a separate step so you can see the working.  You can combine this of course.

data abcl;
  input id text : & $200.;
cards;
101 Prime Minister has arrived in Melbourne for the final leg of his four-city tour of Australia on 01 Mar 2014  and 03 Apr 2101'
102 The 234/12 advance rfff1111eading of Singapore third quarter on 01 Dec 2012 showing no response
103 There is no symptom minister Malaria after 20 Nov 2014 meaning vaccine work properly
104 The total cost of the conversion is a nonnegative value that is usually seen on 01 Jan 2017 showing 10 people's work
;
run;

data want (drop=i mnths1-mnths12 start);
  set abcl;
  array mnths{12} $5. (" Jan "," Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sep "," Oct "," Nov "," Dec ");
  do i=1 to 12;
    if index(text,mnths{i}) > 0 then start=index(text,mnths{i}) - 3;
  end;
  if start > 0 then dte=input(compress(substr(text,start,12)),date9.);
  if dte > today() then output;
run;

Contributor
Posts: 63

Re: search with text

Thank you very much for looking into the issue. Id 101 should also captured as it contain date (03 Apr 2101)  > current date (20 Nov 2014). Could you please look. Thanks in advance

Super User
Super User
Posts: 7,942

Re: search with text

Yes, that's because I am only searching for 1 date.  How many is there likely to be?  Do you want one row output per date which complies etc?  I don't know your full requirements so you will need to modify a bit.  This should cover multiple dates, but will output once per date fulfilling criteria:

data abcl;
  length text $2000.;
  infile datalines  dlm="¬";
  input id text $;
cards;
101¬Prime Minister has arrived in Melbourne for the final leg of his four-city tour of Australia on 01 Mar 2014  and 03 Apr 2101'
102¬The 234/12 advance rfff1111eading of Singapore third quarter on 01 Dec 2012 showing no response
103¬There is no symptom minister Malaria after 20 Nov 2014 meaning vaccine work properly
104¬The total cost of the conversion is a nonnegative value that is usually seen on 01 Jan 2017 showing 10 people's work
;
run;

data want (drop=i mnths1-mnths12);
  set abcl;
  array mnths{12} $5. (" Jan "," Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sep "," Oct "," Nov "," Dec ");
  array dtes{10} 8.;
  format dtes1-dtes10 date9.;
  c=1;
  do i=1 to 12;
    if index(text,mnths{i}) > 0 then do;
      dtes{c}=input(compress(substr(text,index(text,mnths{i}) - 3,12)),date9.);
      c=c+1;
    end;
  end;
  do i=1 to 10;
    if dtes{i} > today() then output;
  end;
run;

Solution
‎11-20-2014 07:01 AM
SAS Employee
Posts: 15

Re: search with text

Hi.

This should work:

data abcl(keep=id text);

input id text : & $200.;

format date date9.;

if _N_ = 1 then

do;

  retain rId;

  rId = prxparse('/\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4}/i');

end;

start = 1;

stop = length(text);

  

call prxnext(rId, start, stop, text, position, length);

do while (position > 0);

   date = input(substr(text, position, length),date11.);

   call prxnext(rId, start, stop, text, position, length);

   if date > date() then do;

    output;

  continue;

   end;

end;

cards;

101 Prime Minister has arrived in Melbourne for the final leg of his four-city tour of Australia on 01 Mar 2014 and 03 Apr 2101'

102 The 234/12 advance rfff1111eading of Singapore third quarter on 01 Dec 2012 showing no response

103 There is no symptom minister Malaria after 20 Nov 2014 meaning vaccine work properly

104 The total cost of the conversion is a nonnegative value that is usually seen on 01 Jan 2017 showing 10 people's work

;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 246 views
  • 3 likes
  • 3 in conversation