BookmarkSubscribeRSS Feed
Sofiya
Calcite | Level 5

PROC SQL;
CREATE TABLE WORK.Retailer_Frequency_Insval AS
SELECT t1.retailer,
(COUNT(t1.retailer)) AS Retailer_Frequency
FROM SASDATA.insval_history t1
WHERE t1.date >= (intnx('year', "&DT."d, -1))
GROUP BY t1.retailer
ORDER BY t1.retailer;
QUIT;

15 REPLIES 15
Tom
Super User Tom
Super User

No.

It is counting the number of observations from the beginning of last year.

So if the macro variable DT has a string like, 12DEC2022, that the DATE informat can interpret as a date in the year 2022 it counts from the beginning of 2021.

2031  data _null_;
2032    dt='12DEC2022';
2033    date = input(dt,date11.);
2034    index=intnx('year',date,-1);
2035    put (date index) (=date9.);
2036  run;

date=12DEC2022 index=01JAN2021
PeterClemmensen
Tourmaline | Level 20

@Tom , wouldn't it be the number of non-missing values of t1.retailer since the beginning of last year?

Sofiya
Calcite | Level 5

So, it is going to count number of observation for last 12 months even we will be in 2023, right? Is this correct way to count number of observation for the reports?

Sofiya
Calcite | Level 5

Ohh I see. That was my concern. What can I do to run only last 12months from today? 

For example, today is 12/22/22, I believe it should be counting the records from 12/13/2021 to 12/12/2022 which would be 12 months of data.

Kurt_Bremser
Super User

Then you do this:

proc sql;
create table work.Retailer_Frequency_Insval as
  select
    t1.retailer,
    (count(t1.retailer)) as Retailer_Frequency
  from sasdata.insval_history t1
  where t1.date > intnx('year',today(),-1,'s')
  group by t1.retailer
;
quit;

ORDER BY is not necessary, as GROUP BY will result in a sorted order already.

Sofiya
Calcite | Level 5

Tom, I can not see the result. It created the table but there is no value in it.

Sofiya
Calcite | Level 5

@Kurt_Bremser, I ran the code but I can not see the result. It created the table but there is no value in it.

Tom
Super User Tom
Super User

Make sure that the variable you are filtering on (T1.DATE) is actually a DATE variable. 

Make sure that the time period you want is actually there.  Try doing a quick check:

proc freq data=sasdata.insval_history ;
  tables date ;
  format date year4. ;
run;

 

Sofiya
Calcite | Level 5

I can't find the date variable. Where I should go to check it?

Tom
Super User Tom
Super User

@Sofiya wrote:

So, it is going to count number of observation for last 12 months even we will be in 2023, right? Is this correct way to count number of observation for the reports?


It depends.

1) What is the value of the DT macro variable?  Where is that set?  What value does it have?

2) What is the range of dates in the dataset?

 

If you want to count 12 months then say so explicitly in the code.

Something like:

where datevar between intnx('year',"&dt"d,-1,'s') and "&dt"d
Sofiya
Calcite | Level 5

Where can i find micro variable location? I went to Tools -> SAS micro variable viewers ->  searched there, but I could not find the date variable. Is there is another way to find?

Sofiya_0-1670870397138.png

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 15 replies
  • 2503 views
  • 2 likes
  • 4 in conversation