Data Manipulation and Freq

Reply
N/A
Posts: 0

Data Manipulation and Freq

Hi Guys,
I need a code to generate the following output...I am including the data set.

Data set:

System Defect

Cardiovascular Atheroscelorosis
Neurology Vertigo
Cardiovascular Myocardialinfaction
Nephrology Renal Failure
Gastroenterology Gastritis
Gastroenterology Jaundice
Gastroenterology Gastritis
Cardiovascular Myocardialinfaction

Output:
Defect Frequency

Cardiovascular
Atheroscelorosis 1
Myocardialinfaction 2
Nephrology
Renal Failure 1
Gastroenterology
Gastritis 1
Jaundice 1
Gastritis 1

Thanks & Regards
Nani
SAS Employee
Posts: 2

Re: Data Manipulation and Freq

Maybe something like this:

data read;
length system_defect $ 200;
input system_defect &;
system=scan(system_defect,1);
defect=left(tranwrd(system_defect,trimn(system),''));
cards;
Cardiovascular Atheroscelorosis
Neurology Vertigo
Cardiovascular Myocardialinfaction
Nephrology Renal Failure
Gastroenterology Gastritis
Gastroenterology Jaundice
Gastroenterology Gastritis
Cardiovascular Myocardialinfaction
;run;
proc sort;
by system defect;
run;
data result;
set read(drop=system_defect);
by system defect;
if first.defect then count=0;
count+1;
run;


Regards,

Rens, SAS NL
Frequent Contributor
Posts: 91

Re: Data Manipulation and Freq

Change last data step to:
data result;
set read(drop=system_defect);
by system defect;
if first.defect then count=0;
count+1;
if last.defect then output;
run;
SAS Super FREQ
Posts: 8,818

Re: Data Manipulation and Freq

Hi:
Either PROC REPORT or PROC TABULATE will give you a table based on your data.
[pre]
data defects;
length system $20 defect $20;
infile datalines;
input system $ defect $;
return;
datalines;
Cardiovascular Atheroscelorosis
Neurology Vertigo
Cardiovascular Myocardialinfaction
Nephrology Renal Failure
Gastroenterology Gastritis
Gastroenterology Jaundice
Gastroenterology Gastritis
Cardiovascular Myocardialinfaction
;
run;

[/pre]

Then PROC REPORT looks like this in the LISTING window:
[pre]

proc report data=defects nowd;
title 'With Proc Report';
column system defect n;
define system /group;
define defect /group;
run;

The OUTPUT:
With Proc Report

system defect n
Cardiovascular Atheroscelorosis 1
Myocardialinfaction 2
Gastroenterology Gastritis 2
Jaundice 1
Nephrology Renal 1
Neurology Vertigo 1

[/pre]

There are other ways to pretty up the PROC REPORT output, but PROC TABULATE does exactly what you want. And, PROC TABULATE looks like this in the LISTING window:

[pre]
proc tabulate data=defects f=9. noseps
formchar=' ';
title 'With Proc Tabulate';
class system defect;
table system*defect,n='Frequency'/
row=float indent=2 box='Defect'
rts=24;
run;

The OUTPUT:

With Proc Tabulate

Defect Frequency

Cardiovascular
Atheroscelorosis 1
Myocardialinfaction 2
Gastroenterology
Gastritis 2
Jaundice 1
Nephrology
Renal 1
Neurology
Vertigo 1

[/pre]

cynthia
Frequent Contributor
Posts: 95

Re: Data Manipulation and Freq

As Cynthia and others have pointed out, it depends on what you want produced. Here is yet another option (sorry about the look, but I can't seem to produce the nice formatting that others use):

data in;
input (system defect) (:$24.);
cards;
Cardiovascular Atheroscelorosis
Neurology Vertigo
Cardiovascular Myocardialinfaction
Nephrology Renal Failure
Gastroenterology Gastritis
Gastroenterology Jaundice
Gastroenterology Gastritis
Cardiovascular Myocardialinfaction
;
run;

options nocenter;
proc report data=in nowindows;
columns system defect N;
define system / group noprint;
define defect / group;
break after system / skip;
compute before system;
line @3 system $24.;
endcomp;
run;

Output:

defect N
Cardiovascular
Atheroscelorosis 1
Myocardialinfaction 2

Gastroenterology
Gastritis 2
Jaundice 1

Nephrology
Renal 1

Neurology
Vertigo 1
SAS Super FREQ
Posts: 8,818

Re: Data Manipulation and Freq

Hi:
to get the "nice" formatting, use square bracket-pre-square bracket before your code
and
square bracket - /pre-square bracket
after your code

square bracket = [ ]
[-pre-] (without the dashes) and
[-/pre-] (without the dashes)
cynthia
Contributor
Posts: 27

Re: Data Manipulation and Freq

Hi Cynthia ,

The code u have posted actually approximately meets my needs.But the problem I have is I need to get the total count(& percentage as xx.x%) of all the defects persystem along with the defects in each system.
Some thing like this...

CardioVascular 3
Atheroscelorosis 1
Myocardialinfaction 2
Neurology 1
Vertigo 1

And further I need to generate the same as a Dataset or a ODS output..

Thanks & Regards
Rajesh
SAS Super FREQ
Posts: 8,818

Re: Data Manipulation and Freq

Hi:
You can get counts and percents from either PROC REPORT or PROC TABULATE using the N and PCTN statistic.

For more help with the syntax of either procedure or the syntax of the program example below or for help creating a dataset from ODS procedure output, your best bet is to contact SAS Technical Support. Also, many companies that do clinical trial reporting ALREADY have procedures and macro programs that they use to generate the kinds of reports that you ask about. So, before you go too far down the road of ANY procedure, you should check with other programmers at your company to determine what validated programs and methods are already in use.

To contact SAS Technical Support, go to http://support.sas.com/ and in the left-hand navigation pane, click on the link entitled "Submit a Problem".

cynthia

Some example code:
[pre]
data defects;
length system $20 defect $20;
infile datalines;
input system $ defect $;
return;
datalines;
Cardiovascular Atheroscelorosis
Neurology Vertigo
Cardiovascular Myocardialinfarction
Nephrology Renal Failure
Gastroenterology Gastritis
Gastroenterology Jaundice
Gastroenterology Gastritis
Cardiovascular Myocardialinfarction
;
run;

proc format;
picture pct low-high='009.99%';
run;

ods html file='c:\temp\pctrept.html' style=sasweb;

proc tabulate data=defects ;
title 'Percents and Counts with PROC TABULATE';
class system defect;
table system=' ' * (all defect=' ' ) all,
n pctn*f=pct. /
box='system and defect';
run;

proc report data=defects nowd
style(summary)=Header;
title 'Percents and Counts with PROC REPORT';
column system defect n pctn;
define system /group
style(column)=Header;
define defect/group
style(column)=Header;
define n/ 'N' f=comma6.;
define pctn / 'PctN' f=percent8.2;
break before system/ summarize;
compute before system;
defect = 'All';
endcomp;
compute after system;
line ' ';
endcomp;
rbreak after /summarize;
compute after;
system = 'All';
defect = 'All';
endcomp;
run;
ods html close;
[/pre]
Contributor
Posts: 49

Re: Data Manipulation and Freq

Another alternative is that you calculate your frequency counts and totals using a summary procedure (FREQ, SQL, MEANS, UNIVARIATE - take your pick - I've seen lots of different methods) and merge the totals and frequencies together. Calculate %age, create a character variable {xx (xxx%)} and use PROC REPORT to print out the xx (xxx%) variables along with labels, headers, footers, etc.

That's pretty much what I do anyway ... mainly because I dislike TABULATE ...
N/A
Posts: 0

Re: Data Manipulation and Freq

I do have a problem similar to that of Mr.Rajesh.Can anybody help me out?

Regards,
Nani
Contributor
Posts: 27

Re: Data Manipulation and Freq

Thanks for the replies guys..

cheers,
rajesh
Ask a Question
Discussion stats
  • 10 replies
  • 755 views
  • 0 likes
  • 7 in conversation