DATA Step, Macro, Functions and more

To create a frequency distribution table

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

To create a frequency distribution table

Hi to All,

I am a new SAS user and I’m trying to produce the output below using the following set of codes, where the non-numeric outcomes are missing values, A=” Not Home” and R=” Refused.” The problem I’m having is that my SAS program displays the percent, cumulative frequency, cumulative percent of the missing variables. Is there a way to exclude both missing variables (A and R) in calculating these three measures and report them as missing values as in the table below?

Thank you.

Lear Alfonso

 

                                                        The FREQ Procedure

 

                                                      Cumulative    Cumulative

                        q1    Frequency     Percent          Frequency         Percent

                  ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

                  Not home           2          .                          .                     .

                  Refused             1          .                          .                     .

                         1                 3       42.86                    3                 42.86

                         2                 3       42.86                    6                 85.71

                         3                 1       14.29                    7               100.00

 

Frequency Missing = 3

 

DATA survey;

      MISSING A R;

      INPUT id q1 $;

CARDS;

8401 2

8402 A

8403 1

8404 1

8405 2

8406 3

8407 A

8408 1

8408 R

8410 2

;

 

RUN;

 

 

Data surveyf;

set survey;

 

if q1='a' then q1='.';

if q1='r' then q1='.';

*;

proc format;

value $qf 'A' = 'Not Home' 'R' = 'Refused';

format q1 $qf.;

run;

 

 proc freq data=surveyf;

 format q1 $qf.;

 tables q1 / missprint;

 RUN;

 

quit;

 

My SAS output

  

 

The FREQ Procedure

q1 Frequency Percent Cumulative
Frequency Cumulative
Percent
1330.00330.00
2330.00660.00
3110.00770.00
Not Home220.00990.00
Refused110.0010100.00

 


Accepted Solutions
Solution
‎09-23-2017 05:04 PM
Super User
Posts: 19,878

Re: To create a frequency distribution table

Can you use .A and .R instead and treat the values as numerics?

View solution in original post


All Replies
Solution
‎09-23-2017 05:04 PM
Super User
Posts: 19,878

Re: To create a frequency distribution table

Can you use .A and .R instead and treat the values as numerics?

Super User
Posts: 10,046

Re: To create a frequency distribution table

DATA survey;
INPUT id q1 : $20.;
if q1='A' then q=.n;
 else if q1='R' then q=.r;
  else q=input(q1,best.);
CARDS;
8401 2
8402 A
8403 1
8404 1
8405 2
8406 3
8407 A
8408 1
8408 R
8410 2
;
run;
proc format;
value q .n = 'Not Home' .r = 'Refused';
run;
proc freq data=survey;
table q/missprint;
format q q.;
run;
 
New Contributor
Posts: 4

Creating a frequency distribution table

Hi to All,

I am a new SAS user and I’m trying to produce the output below using the following set of codes, where the non-numeric outcomes are missing values, A=” Not Home” and R=” Refused.” The problem I’m having is that my SAS program displays the percent, cumulative frequency, cumulative percent of the missing variables. Is there a way to exclude both missing variables (A and R) in calculating these three measures and report them as missing values as in the table below?

Thank you.

Lear Alfonso

 

                                                        The FREQ Procedure

 

                                                      Cumulative    Cumulative

                        q1    Frequency     Percent          Frequency         Percent

                  ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

                  Not home           2          .                          .                     .

                  Refused             1          .                          .                     .

                         1                 3       42.86                    3                 42.86

                         2                 3       42.86                    6                 85.71

                         3                 1       14.29                    7               100.00

 

Frequency Missing = 3

 

DATA survey;

      MISSING A R;

      INPUT id q1 $;

CARDS;

8401 2

8402 A

8403 1

8404 1

8405 2

8406 3

8407 A

8408 1

8408 R

8410 2

;

 

RUN;

 

 

Data surveyf;

set survey;

 

if q1='a' then q1='.';

if q1='r' then q1='.';

*;

proc format;

value $qf 'A' = 'Not Home' 'R' = 'Refused';

format q1 $qf.;

run;

 

 proc freq data=surveyf;

 format q1 $qf.;

 tables q1 / missprint;

 RUN;

 

quit;

 

My SAS output

  

 

The FREQ Procedure

q1 Frequency Percent Cumulative
Frequency Cumulative
Percent
1330.00330.00
2330.00660.00
3110.00770.00
Not Home220.00990.00
Refused110.0010100.00

 

Super User
Posts: 5,518

Re: Creating a frequency distribution table

While there are errors in your program, the errors don't affect the answer to your question.  You can't get the table you want with PROC FREQ.  You can get part-way there if you were to create Q1 as a numeric variable.  MISSPRINT still gives you counts (not percents) for the missing values.  (I can't test it right now, but you can try the program below.)

 

Here are some changes that might get you a bit closer:

 

data survey; 

missing A R; 

input id q1; 

cards; 

....... same data lines ...

;

 

proc format;

 value qf .A = 'Not Home'  .R = 'Refused';

run;

 

proc freq data=surveyf;

 format q1 qf.;

  tables q1 / missprint;

run;

Super User
Posts: 7,868

Re: Creating a frequency distribution table

I merged the two threads concerning the same issue.

Please do not double-post.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 4

Re: Creating a frequency distribution table

Posted in reply to KurtBremser

Thanks, I will take note.

Lear0010

New Contributor
Posts: 4

Re: To create a frequency distribution table

Thanks for your quick responses.  I revised the program as suggested by Reeza, Ksharp, and Astounding, and they all worked!

Lear0010

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 209 views
  • 0 likes
  • 5 in conversation