BookmarkSubscribeRSS Feed
Lattecoffeegirl
Fluorite | Level 6

Hi, I tried to find help but didnt found a good solution.

How can I merge several proc freq tables?

 

I will try to make sample data, but maybe the question is so easy, that it can be answered without.

My output out of a simple prc freq procedure:

 

Var1  Frequency
1        2
2 3
3 5
4 8

Var2 Frequency
1 1
2 2
3 7
4 1

 

now I just want to get 1 table where in the colums the responses 1, 2, 3, 4 and in the rows the variables are listed.

So kind of:

 

         1    2    3    4
Var1     2    3    5    8
Var2     1    2    7    1

 

Thanks 🙂

 

2 REPLIES 2
ballardw
Super User

Do you want a report? That is relatively simple.

data one;
  input Var1  Frequency;
  length varname $ 32.;
  varname = "Var1";
  value=var1;
datalines;
1        2
2        3
3        5
4        8
;

data two;
  input Var2  Frequency;
  length varname $ 32.;
  varname = "Var2";
  value = var2;
datalines;
1        1
2        2
3        7
4        1
;

data combined;
   set one two;
run;


proc tabulate data=combined;
class varname value;
freq frequency;
table varname='',
value=''*n=''
;
run;

 

If you want a data set you have several problems, first 1,2,3 and 4 are not valid variable names. The data set Combined above is about as close as you should come. If you need to display values like that the extra stuff to have non-standard variable names like "1" is generally not worth the effort in my opinion.

 

Question: do these come from the same data set with different proc freq outputs? If so, show the code you are using as we can likely create the output from Proc Freq that comes closest to what you want.

Ksharp
Super User

You want a report or a dataset ? If you want report ,ballarw give you answer,if you want dataset try this:

data one;
  input Var1  Frequency;
datalines;
1        2
2        3
3        5
4        8
;

data two;
  input Var2  Frequency;
datalines;
1        1
2        2
3        7
4        1
;


data temp;
 merge one(rename=(var1=id Frequency=var1)) 
       two(rename=(var2=id Frequency=var2));
 by id;
run;

proc transpose data=temp out=want;
id id;
var var1 var2;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 976 views
  • 0 likes
  • 3 in conversation