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;
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.
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;
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.
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.
@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.
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.
Perfect! Thank you so much.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: