DATA Step, Macro, Functions and more

Show missing variable with 0

Reply
Regular Contributor
Posts: 178

Show missing variable with 0

data test;

input id days;

datalines;

1  30;

2 90

3 115

;

run;

 

data test2;

set test1;

if days >=0 and days <=30 then do; range = '0-30'; else

if days >=31 and days <=60 then range ='31-60'; else

if days >=61 and days <=90 then range = '61-90';else

if days >=91 and days <=120 then range = '91-120;

cnt = 1;

run;

In this example nothing would show for the 31-60 category however in that case I want to to show 31-60 with a cnt =0 to reflect this so I can use it in a proc tabulate later on in the program. 

PROC Star
Posts: 1,351

Re: Show missing variable with 0

use proc formats rather than if then. If then is rather tedious and boring in this case. Well, i wouldn't do it 

Super User
Posts: 22,874

Re: Show missing variable with 0

Deal with that within PROC TABULATE using PRELOADFMT. 

 

If you search those two terms you'll find plenty of examples on how to use it. It will also save you the recoding step, since you can apply the format in PROC TABULATE directly, though you could still recode if you wanted to. 

Trusted Advisor
Posts: 1,826

Re: Show missing variable with 0

You test data soesn't give enough informatio what you want to count.

 

Check and addapt next code to your needs:

proc format lib=work;
value range
  low - 30 = '0-30'
   30 - 60 = '31-60' 
   60 - 90 = '61-90'
   90 - 120= '91-120'
   other = '***'
  ;
run;

data skilton;
  retain cnt 1;
  length range $6;
  range = '0-30'; output;
  range = '31-60'; output;  
  range = '61-90'; output;  
  range = '91-120'; output;
run;

data test;
input id days;
datalines;
1 30
2 90
3 115
;
run;

data test2;
set test;
    length range $6;
    range = put(days,range.);
run;
proc sort data=test2; by range; run;

data to_report;
merge skilton test2;
  by range;
  if id=. then cnt=0;
  drop days;
run;  
Ask a Question
Discussion stats
  • 3 replies
  • 102 views
  • 1 like
  • 4 in conversation