DATA Step, Macro, Functions and more

How to get SQL proc output to a micro variable vector?

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

How to get SQL proc output to a micro variable vector?

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

 

 

 


Accepted Solutions
Solution
‎02-16-2017 12:12 PM
Regular Contributor
Posts: 194

Re: How to get SQL proc output to a micro variable vector?

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;

View solution in original post


All Replies
Solution
‎02-16-2017 12:12 PM
Regular Contributor
Posts: 194

Re: How to get SQL proc output to a micro variable vector?

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;
Occasional Contributor
Posts: 13

Re: How to get SQL proc output to a micro variable vector?

[ Edited ]

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;

 

 

SAS Employee
Posts: 20

Re: How to get SQL proc output to a micro variable vector?

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

Regular Contributor
Posts: 194

Re: How to get SQL proc output to a micro variable vector?

This seems to work :

 

proc sql noprint;
  select catx(' ',name, count(*))
  into :names separated by ','
  from sashelp.class
  group by name 
  ;
quit;

%put &names.;
Contributor
Posts: 26

Re: How to get SQL proc output to a micro variable vector?

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

Contributor
Posts: 26

Re: How to get SQL proc output to a micro variable vector?

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

Super User
Posts: 10,500

Re: How to get SQL proc output to a micro variable vector?

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.

Contributor
Posts: 26

Re: How to get SQL proc output to a micro variable vector?

[ Edited ]

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  

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 198 views
  • 2 likes
  • 5 in conversation