Hi i have returned SQL output using macro, but my results are returned as city name concatenated as a string and customers count in each city as a single concatenated string all are separated by space as code is given below.
But i want these strings as a vector of cities and a vector of customer counts to create a data in SAS. Please help me
proc sql NOPRINT;
select city, count(*) into :cit seperated by ' ',
:fre seperated by ' '
from WORK.input
WHERE customer= 1
group by city;
quit;
%put &cit;
%put &fre;
"
Obs city
1 Baar Basel Biel Cham Dietikon Dübendorf Geneva Kloten Lausanne Luzein Neuchactel OTHER Olten Pfäffikon Rotkreuz
Obs frequ
1 13 14 2 2 4 2 47 5 5 6 1 119 1 2 1 3 2 3 2 19 246
"
I want to generate sas data as
Baar 13
Basel 14
.
.
.
Rotkreuz 246
Hello,
I am not sure I understand your question.
If you want to create a dataset that contains the number of occurrences of each city name
for customer 1 then you can create the dataset directly in the proc sql without having
to export your data into macrovariables :
proc sql noprint;
CREATE TABLE want AS
SELECT CITY, count(*) AS CITY_COUNT
FROM WORK.input
WHERE customer= 1
GROUP BY city;
quit;
Hello,
I am not sure I understand your question.
If you want to create a dataset that contains the number of occurrences of each city name
for customer 1 then you can create the dataset directly in the proc sql without having
to export your data into macrovariables :
proc sql noprint;
CREATE TABLE want AS
SELECT CITY, count(*) AS CITY_COUNT
FROM WORK.input
WHERE customer= 1
GROUP BY city;
quit;
Good morning,
I am not sure I understand your question.
If you want to print your result, i propose this solution:
DATA _null_;
SET sashelp.class end=last;
call symputx ('col1'||LEFT(_n_),LEFT(name));
call symputx ('col2'||LEFT(_n_),LEFT(sex));
if last then call symput ('nbr',_n_);
run;
%macro mamacro();
DATA _null_;
put "name" %str(' ') "Sex";
%do i=1 %to &nbr;
put "&&col1&i" %str( ' ' ) "&&col2&i" ;
%end;
run;
%mend ;
%mamacro;
I don't think it is possible in one pass, you need to count in a sub-select and then create your macro variable string.
proc sql noprint;
select catx(' ',name, counts)
into : names separated by ','
from (
select name, count(*) as counts
from sashelp.class
group by name )
;
quit;
%put &names ;
Which would give:
%put &names ;
Alfred 1,Alice 1,Barbara 1,Carol 1,Henry 1,James 1,Jane 1,Janet 1,Jeffrey 1,John 1,Joyce 1,Judy
1,Louise 1,Mary 1,Philip 1,Robert 1,Ronald 1,Thomas 1,William 1
This seems to work :
proc sql noprint;
select catx(' ',name, count(*))
into :names separated by ','
from sashelp.class
group by name
;
quit;
%put &names.;
Thanks for your kind reply, I got some success given below.
Baar 13,Basel 14,Biel 2,Cham 2,Dietikon 4,Dübendorf 2,Geneva 47,Kloten 5,Lausanne 5,Luzein 6,Neuchactel 1,OTHER 119,Olten
1,Pfäffikon 2,Rotkreuz 1,Schaffhausen 3,St. Gallen 2,Wallisellen 3,Winterthur 2,Zug 19,Zurich 246
But i need output as given below. So that i can use the output for bar plot of city with the given value.
Obs city COUNT
1 Baar 13
2 Basel 14
3 Biel 2
4 Cham 2
5 Dietikon 4
6 Dübendorf 2
7 Geneva 47
8 Kloten 5
9 Lausanne 5
10 Luzein 6
11 Neuchactel 1
12 OTHER 119
13 Olten 1
14 Pfäffikon 2
15 Rotkreuz 1
16 Schaffhausen 3
17 St. Gallen 2
18 Wallisellen 3
19 Winterthur 2
20 Zug 19
21 Zurich 246
Hi Thanks a lot for your help, I have got the desired output (given below). As I have started SAS programming just two days ago. I may be poor in basic things about SAS. I found somewhere to use macro variables to hold values for future like plotting, hence, i was trying to that way . But, i still have one problem how can i use this output for bar plot. corresponding to the city and its value.
CITY_
Obs city COUNT
1 Baar 13
2 Basel 14
3 Biel 2
4 Cham 2
5 Dietikon 4
6 Dübendorf 2
7 Geneva 47
8 Kloten 5
9 Lausanne 5
10 Luzein 6
11 Neuchactel 1
12 OTHER 119
13 Olten 1
14 Pfäffikon 2
15 Rotkreuz 1
16 Schaffhausen 3
17 St. Gallen 2
18 Wallisellen 3
19 Winterthur 2
20 Zug 19
21 Zurich 246
For some simple charts such as the count of a category you do not even need to summarize the data. You can request some statistics in the graph procedure.
For example:
proc sort data=work.input; by customer; run; proc sqplot data=WORK.input; by customer; vbar city /stat=freq ; run;
should produce a graph for each customer and vertical bar showing the count (Freq) of each city for that customer.
Things may get crowded it you have a lot of cities though.
You could place a: Where customer=1; in the sgplot to restrict the plot to one customer, or Where customer in (1 ,3, 5); to select specific customers.
Actually, the above-obtained table from the given query in the code you provided, gave me the number of customers for each city, Now, want to plot the number of customers as bar plot for each city. Thanks
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.