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

I have a series of records with dates that appear like "05/23/2022" in the data.  Proc Contents indicates the following:

birth_infantdob Num 8 MMDDYY.

 

My plan was to extract the middle 2 digits (23 in the example) and use a simple IF/THEN:

 

DATA want; SET HAVE;
day_of_month_birth = SUBSTR(birth_infantdob,3,2);		
IF day_of_month_birth > 15 THEN delete;
RUN;

But since the date is numeric, I have to convert it to text before I can use SUBSTR.  But every time I try to convert it, day_of_month_birth is either blank, has a dot (ie, a numeric blank), or equals the number of days since Jan 1 1960.

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Your variable, as indicated by the Format, is a SAS date value. Which means you have access to many functions to extract values such as day of month ,the Day function

So what you request, one way:

DATA want; 
   SET HAVE;
   where day(birth_infantdob) le 15;
RUN;

WHERE, which works with values in the source data set, executes a bit faster and keeps values where the comparison is true.

You can also use

if day(birth_infantdob) > 15 then delete;

if you prefer.


@Wolverine wrote:

I have a series of records with dates that appear like "05/23/2022" in the data.  Proc Contents indicates the following:

birth_infantdob Num 8 MMDDYY.

 

My plan was to extract the middle 2 digits (23 in the example) and use a simple IF/THEN:

 

DATA want; SET HAVE;
day_of_month_birth = SUBSTR(birth_infantdob,3,2);		
IF day_of_month_birth > 15 THEN delete;
RUN;

But since the date is numeric, I have to convert it to text before I can use SUBSTR.  But every time I try to convert it, day_of_month_birth is either blank, has a dot (ie, a numeric blank), or equals the number of days since Jan 1 1960.


 

View solution in original post

3 REPLIES 3
ballardw
Super User

Your variable, as indicated by the Format, is a SAS date value. Which means you have access to many functions to extract values such as day of month ,the Day function

So what you request, one way:

DATA want; 
   SET HAVE;
   where day(birth_infantdob) le 15;
RUN;

WHERE, which works with values in the source data set, executes a bit faster and keeps values where the comparison is true.

You can also use

if day(birth_infantdob) > 15 then delete;

if you prefer.


@Wolverine wrote:

I have a series of records with dates that appear like "05/23/2022" in the data.  Proc Contents indicates the following:

birth_infantdob Num 8 MMDDYY.

 

My plan was to extract the middle 2 digits (23 in the example) and use a simple IF/THEN:

 

DATA want; SET HAVE;
day_of_month_birth = SUBSTR(birth_infantdob,3,2);		
IF day_of_month_birth > 15 THEN delete;
RUN;

But since the date is numeric, I have to convert it to text before I can use SUBSTR.  But every time I try to convert it, day_of_month_birth is either blank, has a dot (ie, a numeric blank), or equals the number of days since Jan 1 1960.


 

Wolverine
Pyrite | Level 9

That is a simple and elegant solution!  Also informative, because I didn't know about the functions to extract information from date variables.  Thanks!

ballardw
Super User

@Wolverine wrote:

That is a simple and elegant solution!  Also informative, because I didn't know about the functions to extract information from date variables.  Thanks!


You may find this helpful: https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

 

Also the many Formats supplied by SAS and that you can make custom formats for date, time and datetime values using Proc Format is helpful as the groups created by formats are usable in reporting and analysis procs and sometimes graphing (kind of picky there).

The INTNX and INTCK functions for incrementing or determining intervals also allow some shifts and offsets to get many common intervals of interest for manipulating dates.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 3 replies
  • 432 views
  • 1 like
  • 2 in conversation