BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

Hi all:

 

I have a dataset which contain several date variables.   In these variables,  I formatted one specific date from 01/01/8888 to 88/88/8888.   After formatting, I am try to search the modified date.   The codes seems not working.   Please help.  Thanks.

 

%let DateVars = culturedate dischargedate admitdate EDadmitdate;

proc format ;
 value NewDate 
  '01JAN8888'd = '88/88/8888'
  other=[mmddyy10.];
run;

proc datasets lib=work nolist;
  	modify have;
  	  format &DateVars. NewDate10. ;
run;
quit;

data want;
    set have;

array datelist(*) &dateVars;

do i=1 to dim(datelist);

if datelist (i) = 88/88/8888;

end; run;
12 REPLIES 12
Reeza
Super User

You may find this helpful:

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

I would probably find that date by checking if the year was 8888.

 

if year(date) = 8888 then ...

@ybz12003 wrote:

Hi all:

 

I have a dataset which contain several date variables.   In these variables,  I formatted one specific date from 01/01/8888 to 88/88/8888.   After formatting, I am try to search the modified date.   The codes seems not working.   Please help.  Thanks.

 

%let DateVars = culturedate dischargedate admitdate EDadmitdate;

proc format ;
 value NewDate 
  '01JAN8888'd = '88/88/8888'
  other=[mmddyy10.];
run;

proc datasets lib=work nolist;
  	modify have;
  	  format &DateVars. NewDate10. ;
run;
quit;

data want;
    set have;

array datelist(*) &dateVars;

do i=1 to dim(datelist);

if datelist (i) = 88/88/8888;

end; run;

 

Shmuel
Garnet | Level 18

As posted already, you can either chek the date itself by:

if datelist(i) = '01JAN888'd

or check the formated value by:

if put(datelist(i), newdate.) = '88/88/888'

Pay attention: while the original date is numeric type, the formated date is character type and need be quoted.

ballardw
Super User
if  VVALUE(datelist (i) ) = '88/88/8888'; 

Another way.

The VVALUE function will return the formatted value of a variable or a variable referenced as an array element.

ybz12003
Rhodochrosite | Level 12

Hi Ballardw:

 

I use the Vvalue function,  I found out it work on single variable, not a whole bunch of list.   Please advice how to fix it.

 

data want_single;
	set have (keep = &DateVars. );

	if  VVALUE(Date1) = '88/88/8888'; 

run;

 

When I use it in the macro, it didn't work.

 

%let DateVars = Date1 Date2 Date3 Date4 Date5;

proc format ;
 value NewDate 
  '01JAN8888'd = '88/88/8888'
  other=[mmddyy10.];
run;

proc datasets lib=work nolist;
  	modify have;
  	format &DateVars. NewDate10. ;
run;
quit;

data want;
	set have (keep = &DateVars. );

	array Datelist(*) &DateVars.;	
	do i=1 to dim(Datelist);	
	if  VVALUE(&DateVars. ) = '88/88/8888'; 

	end;

run;
ballardw
Super User

@ybz12003 wrote:

Hi Ballardw:

 

I use the Vvalue function,  I found out it work on single variable, not a whole bunch of list.   Please advice how to fix it.

 

data want_single;
	set have (keep = &DateVars. );

	if  VVALUE(Date1) = '88/88/8888'; 

run;

 

When I use it in the macro, it didn't work.

 

%let DateVars = Date1 Date2 Date3 Date4 Date5;

proc format ;
 value NewDate 
  '01JAN8888'd = '88/88/8888'
  other=[mmddyy10.];
run;

proc datasets lib=work nolist;
  	modify have;
  	format &DateVars. NewDate10. ;
run;
quit;

data want;
	set have (keep = &DateVars. );

	array Datelist(*) &DateVars.;	
	do i=1 to dim(Datelist);	
	if  VVALUE(&DateVars. ) = '88/88/8888'; 

	end;

run;

Wrong syntx. VVALUE(&datevars.) is going to resolve to VVALUE( date1 date2 date3 date4 date5). The function documentation says a SINGLE variable or a SINGLE array reference.

data want;
	set have (keep = &DateVars. );

	array Datelist(*) &DateVars.;	
	do i=1 to dim(Datelist);	
	if  VVALUE(datelist[i] ) = '88/88/8888'; 

	end;

run;

would be the way to compare each element of the array to the value.

Of course you don't indicate what you want to do when the match(es) are found. Your example data step will only have any observations in the output if ALL of the date variables in the array have the tested value.

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @ybz12003 

 

Your date value is still 01JAN8888. The format prints it as 88/88/8888, but your check is against the value, not the formatted value, so it should work if you changed your check to

 

data want;
    set have;    array datelist(*) &dateVars;	do i=1 to dim(datelist);	if datelist (i) = '01jan8888'd;     end;
  run;

 

ybz12003
Rhodochrosite | Level 12

I use the datelist below to do the array, I found nothing.

 

if Datelist(i) = '01JAN8888'd;

No matter is before the formatting or after formatting. 

 

data want;
	set have (keep = &DateVars. );
	array Datelist(*) &DateVars.;
	do i=1 to dim (Datelist);
	if Datelist(i) = '01JAN8888'd;
	end;
run;
Shmuel
Garnet | Level 18

It seems that none of the dates in your data is 01/01/8888 or

maybe the datelist isn't full. Are there more date variables? or

is your dataset empty?

 

please post your full log.

 

ybz12003
Rhodochrosite | Level 12

I have 10K obs in the dataset, and I used ' Proc sort, by date, '.   I could see many '01/01/8888' obs.  However, I could not find it by using array.

Shmuel
Garnet | Level 18

I have created a sample of one observation.

Next code is tested and selects the wanted record.

Assuming you may have more of one date = 01/01/888 in the same observation,

I assigned a flag to check is there any such date.

%let DateVars = culturedate dischargedate admitdate EDadmitdate;

proc format ;
 value NewDate 
  '01JAN8888'd = '88/88/8888'
  other=[mmddyy10.];
run;

data have;
 culturedate = '01jan2000'd;
 dischargedate = '01jan2050'd;
 admitdate   = '01jan8888'd;
 EDadmitdate   = .;
 output;
run;

proc datasets lib=work nolist;
  	modify have;
  	  format &DateVars. NewDate10. ;
run;
quit;
proc contents data=have; run;

data want;
    set have;
    array datelist(*) &dateVars;
    flag=0;
	do i=1 to dim(datelist);
	   dt = datelist(i); put i= dt=;
	   if dt = '01jan8888'd then flag=1;
   end;
   if flag;
run;

Reeza
Super User

Did you read the paper I linked to above? It's very, very helpful here.

 

data want;
	set have (keep = &DateVars. );
	array Datelist(*) &DateVars.;

	do i=1 to dim (Datelist) while(flag ne 0);
	             if year(datelist(i)) = 8888 then flag=1;
	end;


      if flag=1;

run;

@ybz12003 wrote:

I have 10K obs in the dataset, and I used ' Proc sort, by date, '.   I could see many '01/01/8888' obs.  However, I could not find it by using array.


 

Shmuel
Garnet | Level 18

I have created a sample of one observation.

Next code is tested and selects the wanted record.

Assuming you may have more of one date = 01/01/888 in the same observation,

I assigned a flag to check is there any such date.

%let DateVars = culturedate dischargedate admitdate EDadmitdate;

proc format ;
 value NewDate 
  '01JAN8888'd = '88/88/8888'
  other=[mmddyy10.];
run;

data have;
 culturedate = '01jan2000'd;
 dischargedate = '01jan2050'd;
 admitdate   = '01jan8888'd;
 EDadmitdate   = .;
 output;
run;

proc datasets lib=work nolist;
  	modify have;
  	  format &DateVars. NewDate10. ;
run;
quit;
proc contents data=have; run;

data want;
    set have;
    array datelist(*) &dateVars;
    flag=0;
	do i=1 to dim(datelist);
	   dt = datelist(i); put i= dt=;
	   if dt = '01jan8888'd then flag=1;
   end;
   if flag;
run;

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
  • 12 replies
  • 4069 views
  • 2 likes
  • 5 in conversation