SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lear0010
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

View solution in original post

7 REPLIES 7
lear0010
Calcite | Level 5

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

 

Astounding
PROC Star

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;

lear0010
Calcite | Level 5

Thanks, I will take note.

Lear0010

Reeza
Super User

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

Ksharp
Super User
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;
 
lear0010
Calcite | Level 5

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

Lear0010

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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