Creating macro variable from a table

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Creating macro variable from a table


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

 


Accepted Solutions
Solution
‎05-27-2018 03:37 PM
Super User
Super User
Posts: 8,127

Re: Creating macro variable from a table

[ Edited ]

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


All Replies
PROC Star
Posts: 1,845

Re: Creating macro variable from a table

[ Edited ]

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. 

Super User
Posts: 13,583

Re: Creating macro variable from a table

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.

Occasional Contributor
Posts: 15

Re: Creating macro variable from a table

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

Solution
‎05-27-2018 03:37 PM
Super User
Super User
Posts: 8,127

Re: Creating macro variable from a table

[ Edited ]

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.

Occasional Contributor
Posts: 15

Re: Creating macro variable from a table

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. 

Respected Advisor
Posts: 4,743

Re: Creating macro variable from a table

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

Occasional Contributor
Posts: 15

Re: Creating macro variable from a table

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..
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 196 views
  • 2 likes
  • 5 in conversation