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
Amethyst | Level 16

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
Amethyst | Level 16

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



hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1241 views
  • 0 likes
  • 3 in conversation