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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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