data Customers;
input Cust_id Month Spend;
cards;
1 JAN 100
1 FEB 200
1 MAR 300
1 APR
2 JAN 400
2 FEB 100
2 MAR 600
3 JUN 100
;run;
%macro c;
proc sql;
select count(distinct cust_id),Month,sum(Spend) as Total
into :n1,:mth separated by ",",:t1 from Customers ;
quit;
%put &n1 &mth &t1;
%mend c;
%c;
im getting somewhat correct output but not able to get distinct months
i want output as
3 JAN,FEB,MAR,APR,JUN 1800
but im getting output as
3 JAN,FEB,MAR,APR,JAN,FEB,MAR,JUN 1800
how to get distinct months ???????????
and also how to store the result in dataset??
If you want only one copy of each value of MONTH then you need to add the DISTINCT keyword after SELECT.
SAS will happily re-merge the summary statistics for grand total and unique ID counts back onto the detailed observations produced by multiple values of MONTH. So every observation will have the same values for those two columns. But if you don't use the SEPARATED BY clause in the INTO clause then SAS will only place one copy into the macro variable.
data Customers;
input Cust_id Month $ Spend;
monthnum =month(input(cats(1,month,1960),date9.));
cards;
1 JAN 100
1 FEB 200
1 MAR 300
1 APR .
2 JAN 400
2 FEB 100
2 MAR 600
3 JUN 100
;
proc sql noprint;
select distinct
month
, count(distinct cust_id) format=best32.
, sum(spend) format=best32.
, monthnum
into :month_list separated by ' '
, :customers trimmed
, :total trimmed
, :dummy
from customers
order by monthnum
;
quit;
122 %put &=month_list; MONTH_LIST=JAN FEB MAR APR JUN 123 %put &=customers; CUSTOMERS=3 124 %put &=total; TOTAL=1800
I changed it to use space as the delimiter between the multiple MONTH values as commas can be a major pain to use with SAS. Most SAS syntax uses spaces as delimiters and not commas.
You wrote-"
how to get distinct months ???????????
and also how to store the result in dataset??"
I am afraid a single select query will most likely not do that because summary stats will remerge unless you have grouping categorical variables, however you could accomplish using a datastep
In my opinion proc sql is not a great idea for this task
To go even further, If you are working in a production environment, having macro vars with concatenated values from an entire dataset doesn't help the performance. You are better off having a look up dataset.
Your data step is incorrect. You have to read the month as character:
input Cust_id Month $ Spend;
If the other variables are working as needed then a separate query for the months.
HOWEVER since your "month" variable is character valued do you want the result in a specific order? or does
proc sql noprint;
select distinct month into :mth separated by ','
from customers;
quit;
work well enough?
If you need this to sort in calendar order you need to say so. I would expect to see Apr first.
%macro c;
proc sql;
select count(distinct cust_id),sum(Spend) as Total
into :n1,:t1 from Customers ;
quit;
proc sql noprint;
select distinct month into :mth separated by ',' from customers;
quit;
%put &n1 &mth &t1;
%mend c;
for getting months i have used a separate statement for months
but output is coming as
3 APR,FEB,JAN,JUN,MAR 1800
how can i sort the months so that it appears as jan to apr
and 1 more doubt why we are using separate statement for months ,why it is not working in the same statement??
If you want only one copy of each value of MONTH then you need to add the DISTINCT keyword after SELECT.
SAS will happily re-merge the summary statistics for grand total and unique ID counts back onto the detailed observations produced by multiple values of MONTH. So every observation will have the same values for those two columns. But if you don't use the SEPARATED BY clause in the INTO clause then SAS will only place one copy into the macro variable.
data Customers;
input Cust_id Month $ Spend;
monthnum =month(input(cats(1,month,1960),date9.));
cards;
1 JAN 100
1 FEB 200
1 MAR 300
1 APR .
2 JAN 400
2 FEB 100
2 MAR 600
3 JUN 100
;
proc sql noprint;
select distinct
month
, count(distinct cust_id) format=best32.
, sum(spend) format=best32.
, monthnum
into :month_list separated by ' '
, :customers trimmed
, :total trimmed
, :dummy
from customers
order by monthnum
;
quit;
122 %put &=month_list; MONTH_LIST=JAN FEB MAR APR JUN 123 %put &=customers; CUSTOMERS=3 124 %put &=total; TOTAL=1800
I changed it to use space as the delimiter between the multiple MONTH values as commas can be a major pain to use with SAS. Most SAS syntax uses spaces as delimiters and not commas.
hi tom,
you are a genius.
the code gives perfect result.
but tom,i had 1 doubt.how
monthnum =month(input(cats(1,month,1960),date9.));
how this statement is working .im not able to understand the functioning of this statement.
And now after getting answers to your question: What are you actually trying to achieve here in the end? WHY are you doing this?
Reason I'm asking: Looks not like a very common coding approach to me which raises the question if there could be a simpler more common approach to solve your actual problem. What's the bigger picture?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.