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

Hi!

I have a dataset where each row is a different person and each column is the date that they visited the study site.

I have list of variables: Pdate1-Pdate1360 and Qdate1-Qdate705. I want to create a flag variable called Flagvar that indicates whether any of the Q dates fell within 30 days of any of the Pdates, but I’m new to arrays and not sure where I’m going wrong. Since I already have over 2000 columns I don't want to create a cartesian product. Here is my code:

data want; set have;

      array Pdate{1360} Pdate1-Pdate1360;

      array Qdate{705} Qdate1-Qdate705;

            do k=1 to 1360;

            do j=1 to 705;

            if intck('day', Pdate{k}, Qdate{j}) le 30

                  then Flagvar=1;

                  else Flagvar=0;

            end;

      end;

run;

 

Suggestions, please?

Thanks in advance! I'm using SAS 9.4.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Remove the Else condition from your loop. The Else resets the flag as soon as you check the next field. 

 

You may also want to add a LEAVE so you leave the loop when the flag is set to speed this up. Or a while condition to the loop. 

 

Yoir logic and code appears fine otherwise 🙂 

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

Remove the Else condition from your loop. The Else resets the flag as soon as you check the next field. 

 

You may also want to add a LEAVE so you leave the loop when the flag is set to speed this up. Or a while condition to the loop. 

 

Yoir logic and code appears fine otherwise 🙂 

 

 

Astounding
PROC Star

Another issue you may need to address ...

 

Even with ELSE removed, this code gives you the wrong answer.  It properly handles PDates that come before QDates, but not the other way around.  For those cases, INTCK will give you a negative number, which is always less than 30.

 

When measuring days, INTCK isn't really needed.  You can just subtract one from the other.  This might be a better calculation to use:

 

abs(Pdate{k} - Qdate{k})

 

Even then, be wary of missing values which will also be less than 30.

mewsmit
Calcite | Level 5
That's a very good point - thanks!

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

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.

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