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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

2 REPLIES 2
Reeza
Super User

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

Ksharp
Super User

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;


hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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