DATA Step, Macro, Functions and more

Create a summary file on discrete variable values (for a list of variables)

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Create a summary file on discrete variable values (for a list of variables)

Hi Everyone,


I want to create a table that report the number of descrete value for each variables (listed in the name file).
it should look like the table "want"


Since the list of variables I want to create report is 200+ variables, I need to make a code instead of doing it manually.


Thank you for your help.


Merry Christmas and Happy New Year!


HHC

 

data have;
input var1 var2 var3 var4;
datalines;
1 12 300 4
2 12 500 0
0 12 200 2
0 15 200 2
;run;

 

data name;
input name $;
datalines;
var1
var2
var3
;run;

 

data want;
input var1 var2 var3;
datalines;
1 12 300
2 15 500
0 . 200
;run;


Accepted Solutions
Solution
‎12-26-2016 09:48 PM
Super User
Posts: 9,681

Re: Create a summary file on discrete variable values (for a list of variables)


data have;
input var1 var2 var3 var4;
datalines;
1 12 300 4
2 12 500 0
0 12 200 2
0 15 200 2
;run;
 
data name;
input name $;
datalines;
var1
var2
var3
;
run;

data _null_;
 set name end=last;
 if _n_=1 then call execute('proc sql;');
 call execute(cat('create table want_',strip(_n_),' as select distinct(',name,') as ',name,' from have;'));
 if last then call execute('quit;');
run;

data want;
 merge want_:;
run;


View solution in original post


All Replies
Super User
Posts: 17,819

Re: Create a summary file on discrete variable values (for a list of variables)

Maybe only part of what you want, the variables are stacked rather than each in their own column. 

 

ods table onewayfreqs=temp;
proc freq data=sashelp.class;
run;


*Format output;
data want;
length variable $32. variable_value $50.;
set temp;
Variable=scan(table, 2);

Variable_Value=strip(trim(vvaluex(variable)));

keep variable variable_value frequency percent cum:;
label variable='Variable' 
	variable_value='Variable Value';
run;

*Display;
proc print data=want(obs=20) label;
run;

Slight mod from here:

https://gist.github.com/statgeek/e0903d269d4a71316a4e

Solution
‎12-26-2016 09:48 PM
Super User
Posts: 9,681

Re: Create a summary file on discrete variable values (for a list of variables)


data have;
input var1 var2 var3 var4;
datalines;
1 12 300 4
2 12 500 0
0 12 200 2
0 15 200 2
;run;
 
data name;
input name $;
datalines;
var1
var2
var3
;
run;

data _null_;
 set name end=last;
 if _n_=1 then call execute('proc sql;');
 call execute(cat('create table want_',strip(_n_),' as select distinct(',name,') as ',name,' from have;'));
 if last then call execute('quit;');
run;

data want;
 merge want_:;
run;


☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 160 views
  • 0 likes
  • 3 in conversation