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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Vladislaff
SAS Employee

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Abraham
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Vladislaff
SAS Employee

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;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1049 views
  • 3 likes
  • 3 in conversation