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

I have the following goal: To be able to flag any dates in my data file that would have year 2018.

I have 150 date variables so using an array seemed to be the most logical. I do not understand why I am getting the error: Illegal Reference to the Array visitd. How do I make this work? Is there another way to flag dates that are off, based only on year that I am not thinking about? TIA.

 

My code:

Data want;
Set have;
array visitd(150) V1dte --V150dte; /*I actually have each one listed here

format V1dte--V150dte mmddyy10.;
do i=1 to 150;
end;
retain referencedate '01JAN2018'd;
format referencedate mmddyy10.;
if visitd {i} ge referencedate then do;
output;
referencedate = visitd;
end;
Run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Data want;
Set have;
array visitd(150) V1dte --V150dte; 
format V1dte--V150dte mmddyy10.;

flag=0;
do i=1 to 150 while(flag = 0);
    if year(visitd {i}) = 2018 then flag=1;
end;

Run;

Any record with a flag=1 has a date with 2018 in it.

View solution in original post

7 REPLIES 7
Reeza
Super User
You have an end right after your DO loop for some reason and another at the end where it likely should be. Delete the first one. Because you i = 151 at the end of the loop, you're then trying to use your array with 151 which isn't a valid index.
Mscarboncopy
Pyrite | Level 9

Thank you. I tried that but it did not work.

format mba160V1dte--mba160V150dte mmddyy10.;
do i=1 to 150;
retain referencedate '01JAN2018'd;
format referencedate mmddyy10.;
if visitd{i} ge referencedate then do;
output;
referencedate = visitd;
end;
end;  /* the end I removed from above as suggested, must be here as Sas tells me I have one unclosed do block
Run;

Astounding
PROC Star

Let's begin with what you are trying to achieve.  Then we can talk about how to get the result you want.

 

What does it mean to flag each date that is off?  Do you want 150 flags, one for each date?  Do you want to output each "off" date as a separate observation?  What additional variables would need to be in the data set to identify where the "off" date came from?  Your program, even with errors corrected, doesn't make your intention clear.  So specify what the result should be first.

Mscarboncopy
Pyrite | Level 9

Hi. Any date with year 2018 is a mistake in coding. I am trying to generate a file with all of the 2018 dates so I can delete them from my data file. Most dates are before 2018 so it won't be 150 flags. The file has about 150 entries for different ids - some ids have 10 dates, some have 100, some have 150 (and several other variables). Thanks.

Kurt_Bremser
Super User

@Mscarboncopy wrote:

Hi. Any date with year 2018 is a mistake in coding. I am trying to generate a file with all of the 2018 dates so I can delete them from my data file. Most dates are before 2018 so it won't be 150 flags. The file has about 150 entries for different ids - some ids have 10 dates, some have 100, some have 150 (and several other variables). Thanks.


You suffer from a bad data structure. In a case where you have only 10 dates, the space for the other 140 columns is wasted needlessly.

The bigger problem you have is that you need to do complicated coding for a VERY SIMPLE issue.

With a long dataset layout, it is just

data want;
set have;
if Vdte ge '01jan2018'd;
keep ID Vdte; /*add any other variable needed, like SEQ derived from your original column name */
run;

So you should, as a first step in making your life easier, transpose your 150 columns to a long dataset layout, and drop any observations that have missing values.

Reeza
Super User
Data want;
Set have;
array visitd(150) V1dte --V150dte; 
format V1dte--V150dte mmddyy10.;

flag=0;
do i=1 to 150 while(flag = 0);
    if year(visitd {i}) = 2018 then flag=1;
end;

Run;

Any record with a flag=1 has a date with 2018 in it.

Mscarboncopy
Pyrite | Level 9

Perfect! Thank you so much.

 

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
  • 7 replies
  • 2343 views
  • 1 like
  • 4 in conversation