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

Good day.

 

I'm struggling with the problem of creating a report with the last 3 dates that is in my data set. So I should not hard code the dates that I want in the report, the dates should be determined by the data set itself. I've tried using proc sql to get the distinct dates that is present in my data set so I can use the latest 3 dates present, but I can't seem to figure out how to extract only the records with those dates from my data set. the code I used to get the distinct dates is:

proc sql ;
create table DISTINCTQ as
    select distinct QUARTER
        from SASUSER.QUARTER
            order by QUARTER DESC;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

I think this is headed in the right direction.  Here are a couple of changes I would recommend:

 

proc sort data=have out=temp;

by descending quarter;

run;

data want;

set temp;

by descending quarter;

n_dates + first.quarter;

if n_dates > 3 then stop;

drop n_dates;

run;

 

It's not clear how large your data set, and how important efficiency considerations would be.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The problem is your using SQL, which is not built with postional rows in mind, only logical clauses.  Switch to using SAS (it is the language you are programming in for instance) and it is a simple problem:

proc sort data=sasuser.quarter out=distinctq;
  by quarter descending desc;
run;
data want;
  set distinctq;
  by quarter;
  c=ifn(first.quater,1,c+1);
  if c <= 3 then output;
run;
Astounding
PROC Star

I think this is headed in the right direction.  Here are a couple of changes I would recommend:

 

proc sort data=have out=temp;

by descending quarter;

run;

data want;

set temp;

by descending quarter;

n_dates + first.quarter;

if n_dates > 3 then stop;

drop n_dates;

run;

 

It's not clear how large your data set, and how important efficiency considerations would be.

HelpPlease
Calcite | Level 5
thanks a lot! it worked perfectly.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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