BookmarkSubscribeRSS Feed
tSAS1
Obsidian | Level 7

Hello everyone,

 

 I have the below dataset in SAS, and I want to produce the "flag" column:

 

tSAS1_0-1648380965184.png

 

What I want to do is to check each date for each ID if it's between any date_start and date_end or not, if yes then flag will take 1, else 0


Thank you in advance!

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Why is flag = 1 in obs 2 and 0 in obs 3?

tSAS1
Obsidian | Level 7

@PeterClemmensen  for obs=2 for ID=1, date_start=07/12/1998<=date=01/10/2001<=date_end=01/09/2002 ==> flag=1

and in obs=3 for ID=1, date=01/10/2002 > date_end=01/09/2002 ==> date is not included in any ranges of dates of date_start-date_end that's why flag=0

yabwon
Onyx | Level 15

If both start and end dates are null then you want 1.

What if only one of them is null?

 

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



tSAS1
Obsidian | Level 7
@yabwon if both or one of them is missing, we could skip that line (like it is deleted)
yabwon
Onyx | Level 15

Something like this should do the job:

data have;
  call streaminit(123);

  do ID = 1 to 3;
    do date = today()-3 to today()+3;
      mi = rand("integer",-10,10);
      mx = rand("integer",-10,10);
      start_date = date + min(mi,mx);
      end_date   = date + max(mi,mx);
      if date = today() then end_date = .;
      output;
    end;
  end;
  format date start_date end_date yymmdd10.;
  drop mi mx;
run;

data want;
  set have;

  if . < start_date <= end_date then
    flag = start_date <= date <= end_date;

run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 488 views
  • 0 likes
  • 3 in conversation