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

Suppose I have the following:

data table;
input Id Date:YYQ8. Trips Sales;
format Date YYQ8.;
datalines; 
1 2021Q1 10 100
1 2021Q3 2 200
1 2021Q4 4 400
2 2021Q1 16 300
2 2021Q2 8 600
2 2021Q2 19 100;
run;

 

IdDateTripsSales
12021Q110100
12021Q32200
12021Q44400
22021Q116300
22021Q28600
22021Q419100


For each id, I want to insert a blank for each date there wasn't a sale as such:

IdDateTripsSales
12021Q110100
22021Q2..
12021Q32200
12021Q44400
22021Q116300
22021Q28600
22021Q3..
22021Q419100


I want to do this so I can do quarter-over-quarter analysis. How would I accomplish this?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If you have SAS/ETS license, then Proc Timeseries is the right tool.

 

data table;
input Id Date:YYQ8. Trips Sales;
format Date YYQ8.;
datalines; 
1 2021Q1 10 100 
1 2021Q3 2  200 
1 2021Q4 4  400 
2 2021Q1 16 300 
2 2021Q2 8  600 
2 2021Q4 19 100 
;
run;

proc timeseries data = table out = want;
   by ID;
   id Date interval   = quarter
           accumulate = none
           setmiss    = missing
           format     = YYQ8.;
   var Trips Sales;
run;

 

Result:

 

Id  Date    Trips  Sales 
1   2021Q1  10     100 
1   2021Q2  .      . 
1   2021Q3  2      200 
1   2021Q4  4      400 
2   2021Q1  16     300 
2   2021Q2  8      600 
2   2021Q3  .      . 
2   2021Q4  19     100 

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

If you have SAS/ETS license, then Proc Timeseries is the right tool.

 

data table;
input Id Date:YYQ8. Trips Sales;
format Date YYQ8.;
datalines; 
1 2021Q1 10 100 
1 2021Q3 2  200 
1 2021Q4 4  400 
2 2021Q1 16 300 
2 2021Q2 8  600 
2 2021Q4 19 100 
;
run;

proc timeseries data = table out = want;
   by ID;
   id Date interval   = quarter
           accumulate = none
           setmiss    = missing
           format     = YYQ8.;
   var Trips Sales;
run;

 

Result:

 

Id  Date    Trips  Sales 
1   2021Q1  10     100 
1   2021Q2  .      . 
1   2021Q3  2      200 
1   2021Q4  4      400 
2   2021Q1  16     300 
2   2021Q2  8      600 
2   2021Q3  .      . 
2   2021Q4  19     100 
mmaleta851
Fluorite | Level 6
That works. Thank you!
mmaleta851
Fluorite | Level 6

Additionally what happens if I have this

IDDateTripsSales
12021Q25100
12021Q310200

 

And I want to add a blank row before or after to get this?

IDDateTripsSales
12021Q1..
12021Q25100
12021Q310200
12021Q4..

 

PeterClemmensen
Tourmaline | Level 20

If you have specific start / end dates, you can place them in the ID Statement like this

 

proc timeseries data = table out = want;
   by ID;
   id Date interval   = quarter
           accumulate = none
           setmiss    = missing
		   start      = '01jan2021'd
		   end        = '31dec2021'd
           format     = YYQ8.;
   var Trips Sales;
run;
mmaleta851
Fluorite | Level 6
Thank you!
Ksharp
Super User
data table;
input Id Date:YYQ8. Trips Sales;
format Date YYQ8.;
datalines; 
1 2021Q1 10 100 
1 2021Q3 2  200 
1 2021Q4 4  400 
2 2021Q1 16 300 
2 2021Q2 8  600 
2 2021Q4 19 100 
;
run;
proc sql;
create table year as
select id,year(min(date)) as min,year(max(date)) as max from table group by id;
quit;
data yyq;
 set year;
 do year=min to max;
  do qtr=1 to 4;
   date=yyq(year,qtr);output;
  end;
 end;
format date yyq.;
keep id date;
run;
proc sql;
create table want as
select a.*, Trips ,Sales
 from yyq as a left join table as b on a.id=b.id and a.date=b.date;
quit;
s_lassen
Meteorite | Level 14

And if you do not have SAS/ETS, something like this may do the trick:

proc sql;
  create table quarters as select table.id,quarters.date
  from (select distinct date from table) quarters,
       (select distinct id from table) table
  ;
  create table want as select quarters.id,quarters.date,table.trips,table.sales
  from quarters left join table on quarters.id=table.id and quarters.date=table.date
  ;

That is, first select all IDs and QUARTERs from the table, and then left join to get the wanted result. But this will only work if your DATE values are actually the same for each quarter (all the first day in the quarter). 

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
  • 7 replies
  • 1460 views
  • 3 likes
  • 4 in conversation