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


hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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