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

Currently I am studying how to use SQL. Suppose generate month-end observations using DO and OUTPUT in DATA as follows.

%let start="1jan2000"d;
%let finish="31dec2019"d;
data monthend;
format date yymmddn8.;
do date=&start. to &finish.;
if date=intnx("month",date,0,"e") then output;
end;
run;

Can I do the similar thing in SQL too?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Junyong,

 

The undocumented MONOTONIC function might come in handy, but I wouldn't recommend this because it's undocumented and you need a sufficiently large arbitrary dataset (here, e.g., SASHELP.CARS with its 428>=240 observations) as a basis.

proc sql;
create table monthend as
select intnx('month', &start, i, 'e') as date format=yymmddn8.
from (select monotonic()-1 as i from sashelp.cars
      where calculated i<=intck('month', &start, &finish))
order by date;
quit;

It is much better to use a DATA step for this task.

 

Edit:

Just for fun: If you don't want to rely on an existing dataset such as SASHELP.CARS, you can create one yourself, let's call it BASE (although, again, PROC SQL is not the ideal tool for this).

proc sql noprint;
create table seed (x num);
insert into seed
set x=0
set x=1;

select 'seed'||repeat(',seed',int(log2(max(intck('month',&start,&finish),1))-1)) into :ds
from seed;

create table base as
select ' ' from &ds;

create table monthend as
select intnx('month', &start, i, 'e') as date format=yymmddn8.
from (select monotonic()-1 as i from base
      where calculated i<=intck('month', &start, &finish))
order by date;
quit;

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

I'm sure someone can come up with a way to do this in SQL, but it is the wrong tool for the job. Your DATA step is the proper tool to do this kind of looping.

--
Paige Miller
Cynthia_sas
SAS Super FREQ
Hi:
You can't use SQL for looping like this. SQL does not have the concept of iterative DO loops. The DATA step is the correct tool for the job.

Cynthia
PGStats
Opal | Level 21

I don't think you can generate such a series with SAS/SQL without a macro front end. Should be done efficiently with:

 

%let start="1jan2000"d;
%let finish="31dec2019"d;
data monthend;
format date yymmddn8.;
date = intnx("month", &start., 0, "e");
do while(date <= &finish.);
    output;
    date = intnx("month", date, 1, "e");
    end;
run;
PG
Patrick
Opal | Level 21

Possible with Oracle SQL but not SAS SQL.

FreelanceReinh
Jade | Level 19

Hi @Junyong,

 

The undocumented MONOTONIC function might come in handy, but I wouldn't recommend this because it's undocumented and you need a sufficiently large arbitrary dataset (here, e.g., SASHELP.CARS with its 428>=240 observations) as a basis.

proc sql;
create table monthend as
select intnx('month', &start, i, 'e') as date format=yymmddn8.
from (select monotonic()-1 as i from sashelp.cars
      where calculated i<=intck('month', &start, &finish))
order by date;
quit;

It is much better to use a DATA step for this task.

 

Edit:

Just for fun: If you don't want to rely on an existing dataset such as SASHELP.CARS, you can create one yourself, let's call it BASE (although, again, PROC SQL is not the ideal tool for this).

proc sql noprint;
create table seed (x num);
insert into seed
set x=0
set x=1;

select 'seed'||repeat(',seed',int(log2(max(intck('month',&start,&finish),1))-1)) into :ds
from seed;

create table base as
select ' ' from &ds;

create table monthend as
select intnx('month', &start, i, 'e') as date format=yymmddn8.
from (select monotonic()-1 as i from base
      where calculated i<=intck('month', &start, &finish))
order by date;
quit;

 

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 860 views
  • 1 like
  • 6 in conversation