BookmarkSubscribeRSS Feed
supersonic
Calcite | Level 5

I have a dataset 'test' as

  var1 var2 var3 var4....varn

  abcd M 22 1500...

  qwerty M 32 5000...

  asdf F 21 100...

  tyuif F 33 6500..

  jklmn M 45 800..

  fghtn F 18 4000..

  fgtn F 28 4050..

  abcd M 22 1500..

  abcde M 24 1500..

I need to get the output as

for only numeric variables

Header 1Header 2Header 3Header 4Header 5
VariableMinMaxMeanStd
Var3
Var4
...
Varn

for only character variables

Header 1Header 2Header 3Header 4
VariableModeFreqency of ModeNo:of distinct varaibles
var1
var2
..
varn
7 REPLIES 7
supersonic
Calcite | Level 5


The first part can be done using proc means.

Please help to solve the 2nd part

Fugue
Quartz | Level 8

Have you taken a look at PROC TRANSPOSE?

supersonic
Calcite | Level 5

no i havent tried.

But whether proc transpose will give me the mode , freq of mode and distinct count?

supersonic
Calcite | Level 5

any clue?

Murray_Court
Quartz | Level 8

This should work, I have solved for the numeric variables and left the char one as an exercise.

options symbolgen mlogic mprint; /* for debugginf macros, but the good stuff into the log */

data test; /* example dataset */

input string $ 1-4 gender $6 age 8-9 stat 10-14;

datalines;

abcd M 22 1500

qwer M 32 5000

asdf F 21 100

tyui F 33 6500

jklm M 45 800

fght F 18 4000

fgtn F 28 4050

;

data nums; /*subset numeric*/

set test(keep=_numeric_);

run;

data chars;/*subset character*/

set test(keep=_character_);

run;

/*get list of numeric variable names, shove them into a macro */

proc transpose data=nums out=numst;

run;

proc sql;

select _NAME_ into :numvars seperated by "*" from numst;

quit;

%let cardnumvars= %sysfunc(countw(&numvars.,*));

%put numvars.=&numvars;

%put cardnumvars.=&cardnumvars.;

%macro numstats;

proc sql;

/*for each numeric variable create table with the stats */

%do i=1 %to &cardnumvars.;

%let var =  %scan(&numvars.,&i.);

create table num&i. as

select

"&var   " as variable, /* hacking to stop truncation of name variable, needs better fix for general use */

min(&var) as min,

max(&var) as max,

avg(&var) as mean,

std(&var) as std

from nums

;

%end;

quit;

%mend numstats;

%numstats

/*put all the small stats tables together */

%macro lister;

%do i=1 %to &cardnumvars.;

num&i.

%end;

%mend lister;

data numstats;

set %lister ;

run;

/* print the output */

proc print data=numstats;

run;

/* similar proccess can be used for character variables, left as exercise */

Astounding
PROC Star

Here's one way to get the Frequency of Mode and Number of Values for a single variable.  There are other methods, such as SQL.  I leave it up to you to get them into your report properly.

proc freq data=have order=freq;

  tables var1 / noprint out=var1;

run;

data want;

  set var1 (obs=1) nobs=_nobs_;

  mode = var1;

  frequency_of_mode = count;

  number_of_distinct_values = _nobs_;

run;

Also note that PROC MEANS can compute the MODE.  However, you may have to make a decision about what to report if two values are tied for the highest frequency.

Good luck.

supersonic
Calcite | Level 5

Thank you Astounding and Murray_Court


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
  • 7 replies
  • 1180 views
  • 6 likes
  • 4 in conversation