DATA Step, Macro, Functions and more

Proc format issue?

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

Proc format issue?

/*Hi Forum,

I have the below data set. I wanted to create some income bands. To achieve this, I created a format and applied on the dataset.*/

 

data have;

input income;

cards;

0

15

392

219

95

.

208

1020

12

41

22

65

372

360

0

0

0

393

190

0

0

0

168

93

890

0

14

43

138

52

125

0

24

45

;

run;

 

 

 

 

/*Creating a format*/

proc format fmtlib;

value val

. = 'Missing'

low-< 0 = '<0'

0 = '0'

1 - 6 = '1 - 6'

>6 - 12 = '>6 - 12'

>12 - 60 = '>12 - 60'

>60 - 120 = '>60 - 120'

>120 - 240 = '>120 - 240'

>240 - 360 = '>240 - 360'

>360 - 480 = '>360 - 480'

>480 - 600 = '>480 - 600'

>600 - 720 = '>600 - 720'

>720 - 840 = '>720 - 840'

>840 - 960 = '>840 - 960'

>960 - 1080 = '>960 - 1080'

>1080 - 1200 = '>1080 - 1200'

>1200 - high = '>1200'

;

run;

 

/*Creating income bands by Applying the format */

data want;

set have;

Income_band= put (Income, val.);

run;

 

 

/*Q: At the end of the Income_band variable that was created, I am getting this notation, which is ">".

Would you help me to get rind of that notation?

Thanks

Mirisa

 

Missing

<0

0

1-6 >

>6-12 >

>12-60 >

>60-120 >

>120-240 >

>240-360 >

>360-480 >

>480-600 >

>480-600 >

>600-720 >

>720-840 >

>840-960 >

>960-1080 >

>1080-1200 >

>1200

 

*/

 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 5,081

Re: Proc format issue?

Uses uses a funny-looking syntax to exclude the left-hand endpoint from a range:

 

value val

. = 'Missing'

low-< 0 = '<0'

0 = '0'

1 - 6 = '1 - 6'

6 <- 12 = '>6 - 12'

12 <- 60 = '>12 - 60'

60 <- 120 = '>60 - 120'

120 <- 240 = '>120 - 240'

......

View solution in original post


All Replies
Super User
Super User
Posts: 7,395

Re: Proc format issue?

As your data seems to be integer shouldn't the bands be:

proc format fmtlib;
  value val
    . = 'Missing'
    low--1 = '<0'
    0 = '0'
    1 - 6 = '1 - 6'
    7 - 12 = '>6 - 12'
    13 - 60 = '>12 - 60'
...
;
run;

I.e. if its not 1-6 then it starts at 7 not > 6.

Solution
2 weeks ago
Super User
Posts: 5,081

Re: Proc format issue?

Uses uses a funny-looking syntax to exclude the left-hand endpoint from a range:

 

value val

. = 'Missing'

low-< 0 = '<0'

0 = '0'

1 - 6 = '1 - 6'

6 <- 12 = '>6 - 12'

12 <- 60 = '>12 - 60'

60 <- 120 = '>60 - 120'

120 <- 240 = '>120 - 240'

......

Super User
Super User
Posts: 7,395

Re: Proc format issue?

But isn't, when we talk about integer data, 

6 <- 12

Just a complicated way of saying

7 - 12

??

Super User
Posts: 5,081

Re: Proc format issue?

Absolutely, if the incoming data always contains integer then it's simpler to get rid of the ">".  I couldn't be sure that the data would always contain integers (who knows what the future holds?), so I chose to use a more robust approach.

New Contributor
Posts: 3

Re: Proc format issue?

Below is the modified code:

 

data have;
input income;
cards;
0
15
392
219
95
.
208
1020
12
41
22
65
372
360
0
0
0
393
190
0
0
0
168
93
890
0
14
43
138
52
125
0
24
45
;
run;


proc format fmtlib;
value val
. = 'Missing'
low-< 0 = '<0'
0 = '0'
1<-6 = '1 - 6'
6<-12 = '>6 - 12'
12<-60 = '>12 - 60'
60<-120 = '>60 - 120'
120<-240 = '>120 - 240'
240<-360 = '>240 - 360'
360<-480 = '>360 - 480'
480<-600 = '>480 - 600'
600<-720 = '>600 - 720'
720<-840 = '>720 - 840'
840<-960 = '>840 - 960'
960<-1080 = '>960 - 1080'
1080<-1200 = '>1080 - 1200'
1200<-high = '>1200'
;
run;


data want;
set have;
format income val.;
run;


proc print data=want;
run;
 

 

The code that you used included, ex: <1200-high for the assignment for the format, which is why SAS was printing '>1200>'. Instead you need to put the syntax 1200<-high and apply for the code.  After that you used a put statement, which is the wrong code to use for this situation.  Use the format statement instead to apply the format val. that you just created.  If you wanted to permanently associate the format with the data, then you need to create a new variable (incomechar in the code below) and use the put function like you did initially. 

 

data want;
set have;
incomechar=put(income,val.);
run;

 

Hope this helps.

Contributor
Posts: 31

Re: Proc format issue?

Hi Astounding, RW3 and bgoth1,

Thanks to each one of you for the help.

Regards

Mirisa

 

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 132 views
  • 4 likes
  • 4 in conversation