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

I would like to delete the first n-obs or rows based on the date, but I am unsure how. The DATE variable is numeric and FORMAT IS YYQ6 (ex. 2013Q1). 

Here is the code I have at the moment

 

PROC SORT DATA = HAVE;
BY DATE ;
RUN;

 

DATA HAVE;
SET HAVE;
IF VAR1 = 0 THEN DELETE;
IF DATE = 2013Q4 THEN DELETE;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
DATA want;

SET HAVE;

IF VAR1 = 0 THEN DELETE;

IF put(DATE, YYQ6.) = "2013Q4" THEN DELETE;

if year(date) = 2013 and qtr(date) = 4 then delete;

RUN;

FYI - do not use this style of programming. It's harder to debug and find any issues in your programming, instead give your output data set a unique name as above otherwise you cannot trace your work between steps.

DATA HAVE;
SET HAVE;

@pbhatt wrote:

I would like to delete the first n-obs or rows based on the date, but I am unsure how. The DATE variable is numeric and FORMAT IS YYQ6 (ex. 2013Q1). 

Here is the code I have at the moment

 

PROC SORT DATA = HAVE;
BY DATE ;
RUN;

 

DATA HAVE;
SET HAVE;
IF VAR1 = 0 THEN DELETE;
IF DATE = 2013Q4 THEN DELETE;

RUN;


 

View solution in original post

3 REPLIES 3
Reeza
Super User
DATA want;

SET HAVE;

IF VAR1 = 0 THEN DELETE;

IF put(DATE, YYQ6.) = "2013Q4" THEN DELETE;

if year(date) = 2013 and qtr(date) = 4 then delete;

RUN;

FYI - do not use this style of programming. It's harder to debug and find any issues in your programming, instead give your output data set a unique name as above otherwise you cannot trace your work between steps.

DATA HAVE;
SET HAVE;

@pbhatt wrote:

I would like to delete the first n-obs or rows based on the date, but I am unsure how. The DATE variable is numeric and FORMAT IS YYQ6 (ex. 2013Q1). 

Here is the code I have at the moment

 

PROC SORT DATA = HAVE;
BY DATE ;
RUN;

 

DATA HAVE;
SET HAVE;
IF VAR1 = 0 THEN DELETE;
IF DATE = 2013Q4 THEN DELETE;

RUN;


 

pbhatt
Calcite | Level 5
Thanks so much! It worked! Also, I tried another method as well which also worked:
DATA WORK.TEST1;
SET HAVE;
IF VAR1 = 0 THEN DELETE;
IF DISCHARGE < '01OCT2013'D THEN DELETE;
RUN;

Also, thanks again Reeza for the tips. I will make sure to use better syntax so I can trace my steps/mistakes and troubleshoot.
Reeza
Super User

Your code technically works but it deletes anything prior to Q4, not just in the third quarter which is what your original code was trying to do.
Only you know which one is right, as this isn't a technical question.

 

You can simplify it slightly if desired as well.

 

DATA WORK.TEST1;
SET HAVE;
IF VAR1 = 0 or  DISCHARGE < '01OCT2013'D THEN DELETE;
RUN;



SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2253 views
  • 0 likes
  • 2 in conversation