Hi all,
I'm a SAS newbie! I have a very large dataset and it looks similar to this:
var1 var2 var3 var4 var5
10384 35 1 5 1
10383 56 2 3 2
10567 76 1 4 5
Var5 indicates the number of rows with the same information so it really is this:
var1 var2 var3 var4
10384 35 1 5
10383 56 2 3
10383 56 2 3
10567 76 1 4
10567 76 1 4
10567 76 1 4
10567 76 1 4
10567 76 1 4
How do I find the frequency for var1, var2, var3, and var4 without expanding the dataset?
data have;
input var1 var2 var3 var4 var5 ;
cards;
10384 35 1 5 1
10383 56 2 3 2
10567 76 1 4 5
;
run;
proc freq data=have noprint;
table var1/out=want1 ;
table var2/out=want2 ;
weight var5;
run;
A simple transpose gives
proc transpose data=have out=w;
by var5;
var var1-var4;
run;
proc print noobs;run;
gives
1 var1 10384
1 var2 35
1 var3 1
1 var4 5
2 var1 10383
2 var2 56
2 var3 2
2 var4 3
5 var1 10567
5 var2 76
5 var3 1
5 var4 4
I'm not clear with your expected output yet. Do you want the frequencies of each value of var1 -- var4 or number of records in var1,var2 and var4?
If I were to assume it is frequencies of each value of var1-var4,
proc sql;
create table want as
select col1, sum(var5) as freq
from w
group by col1;
quit;
gives
COL1 freq
1 6
2 2
3 2
4 5
5 1
35 1
56 2
76 5
10383 2
10384 1
10567 5
data have;
input var1 var2 var3 var4 var5 ;
cards;
10384 35 1 5 1
10383 56 2 3 2
10567 76 1 4 5
;
run;
proc freq data=have noprint;
table var1/out=want1 ;
table var2/out=want2 ;
weight var5;
run;
I see that I needed to use "weight". It worked perfectly! Thanks.
data have;
infile cards missover;
input var1 var2 var3 var4;
cards;
10384 35 1 5
10383 56 2 3
10383 56 2 3
10567 76 1 4
10567 76 1 4
10567 76 1 4
10567 76 1 4
10567 76 1 4
run;
proc freq data=have;
tables var1 var2 var3 var4 / out=want;
run;
proc freq data=have;
tables var1*var2 /out=want2;
run;
Depends on what you really want
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.