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

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;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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