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


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??

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

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. 

ballardw
Super User

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.

adi121
Fluorite | Level 6

%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??

Tom
Super User Tom
Super User

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.

adi121
Fluorite | Level 6

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. 

Patrick
Opal | Level 21

@adi121

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?

adi121
Fluorite | Level 6
hi patrick,
what you are saying is right.
but this question was asked to me at an interview.
So i gave the same reply what you gave .
I could easily do this using data set approach but the interview panel was interested for more complex way.i wonder why they ask complex methods when it can be done in a very simple way..

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 7 replies
  • 830 views
  • 2 likes
  • 5 in conversation