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

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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