sas input v. put functions

65
INPUT VSPUT MSUG | Donna Chan | August 29, 2012 FUNCTIONS

Upload: donna-chan

Post on 07-Aug-2015

301 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: SAS Input v. Put Functions

INPUT VSPUT

MSUG | Donna Chan | August 29, 2012

FUNCTIONS

Page 2: SAS Input v. Put Functions

WIIFM BREW SOME SAS-SY

GOODNESS

INPUT+PUT FUNCTIONS

? ()

Page 3: SAS Input v. Put Functions

WIIFM?

Page 4: SAS Input v. Put Functions

123 abc

Page 5: SAS Input v. Put Functions
Page 6: SAS Input v. Put Functions

INPUT+PUT FUNCTIONS ( )

Page 7: SAS Input v. Put Functions

text in

text out

Page 8: SAS Input v. Put Functions

input ;

text in

text out

Page 9: SAS Input v. Put Functions

input ;

put ;

text in

text out

Page 10: SAS Input v. Put Functions

INPUT/PUT statements and functions

are close cousins.

; ()

Page 11: SAS Input v. Put Functions

input()

put()

text in

text out

Page 12: SAS Input v. Put Functions

input()

put()

text in

text out

Page 13: SAS Input v. Put Functions

input()

new = input(old, informat.) ;

abc123

Page 14: SAS Input v. Put Functions

abc123

date_char

‘05282012’

‘06012012’

‘07052012’

new = input(old, informat.) ;

date_num

19141

19145

19179

input()

Page 15: SAS Input v. Put Functions

abc123

date_char

‘05282012’

‘06012012’

‘07052012’

date_num = input(date_char, mmddyy8.) ;

date_num

19141

19145

19179

input()

Page 16: SAS Input v. Put Functions

abc123

date_char

‘05282012’

‘06012012’

‘07052012’

date_num = input(date_char, mmddyy8.) ;

date_num

19141

19145

19179

input()

Page 17: SAS Input v. Put Functions

abc123

date_char

‘05282012’

‘06012012’

‘07052012’

date_num = input(date_char, mmddyy8.) ;

date_num

19141

19145

19179

input()

E

Page 18: SAS Input v. Put Functions

abc123

date_char

‘05282012’

‘06012012’

‘07052012’

date_num = input(date_char, mmddyy8.) ;

date_num

05/28/2012

06/01/2012

07/05/2012

format date_num mmddyy10. ;

input()

C

Page 19: SAS Input v. Put Functions

abc123

date_char

‘05282012’

‘06012012’

‘07052012’

date_num = input(date_char, mmddyy8.) ;

date_num

05/28/2012

06/01/2012

07/05/2012

format date_num mmddyy10. ;

input()

C

I

Page 20: SAS Input v. Put Functions

abc123

some_num

05282012

06012012

07052012

date_num = input(some_num, mmddyy8.) ;

date_num

?

format date_num mmddyy10. ;

input()

Page 21: SAS Input v. Put Functions

abc123

some_num

05282012

06012012

07052012

date_num = input(some_num, mmddyy8.) ;

date_num

.

.

.

format date_num mmddyy10. ;

input()

Page 22: SAS Input v. Put Functions

abc123

some_num

05282012

06012012

07052012

date_num

.

.

.

format date_num mmddyy10. ;

input()

NOTE: Numeric values have been converted to

character values at the places given by:

(Line):(Column).

date_num = input(some_num, mmddyy8.) ;

Page 23: SAS Input v. Put Functions

Old var is always char.

Format dates.

❶ ❷ ❸

input()

If old var is num, SAS returns note.

abc123

new = input(old, informat.) ;

Informat describes how old var appears—in new var type.

Page 24: SAS Input v. Put Functions

input()

put()

text in

text out

Page 25: SAS Input v. Put Functions

new = put(old, format.) ;

put() 123abc

Page 26: SAS Input v. Put Functions

123abc

ID_num

.

1245

35

659845

new = put(old, format.) ;

ID_char

19141

19145

19179

put()

Page 27: SAS Input v. Put Functions

123abc

ID_char = put(ID_num, z6.) ;

ID_num

.

1245

35

659845

put()

ID_char

‘ .’

‘001245’

‘000035’

‘659845’

Page 28: SAS Input v. Put Functions

123abc

ID_char = put(ID_num, z6.) ;

ID_num

.

1245

35

659845

ID_char

‘ .’

‘001245’

‘000035’

‘659845’

put()

E

Page 29: SAS Input v. Put Functions

123abc

else ID_char = put(ID_num, z6.) ;

ID_num

.

1245

35

659845

ID_char

‘’

‘001245’

‘000035’

‘659845’

if ID_num eq . then ID_char = ‘’ ;

put()

C

Page 30: SAS Input v. Put Functions

123abc

ID_char = put(ID_num, z6.) ;

ID_num

.

1245

35

659845

ID_char

‘ .’

‘001245’

‘000035’

‘659845’

I

put()

Page 31: SAS Input v. Put Functions

123abc

ID_char = put(ID_num, $6.) ;

ID_num

.

1245

35

659845

ID_char

? ’

put()

Page 32: SAS Input v. Put Functions

123abc

ID_num

.

1245

35

659845

ID_char

‘ .’

‘ 1245’

‘ 35’

‘659845’

put()

ID_char = put(ID_num, $6.) ;

Page 33: SAS Input v. Put Functions

123abc

ID_num

.

1245

35

659845

ID_char

‘ .’

‘ 1245’

‘ 35’

‘659845’

WARNING: Variable ID_num has already

been defined as numeric.

put()

ID_char = put(ID_num, $6.) ;

Page 34: SAS Input v. Put Functions

put()

New var is always char.

Hardcode missings.

❶ ❷ ❸ Using new var format type will throw warning.

123abc

new = put(old, format.) ;

Format describes what the new var looks like—in old var type.

Page 35: SAS Input v. Put Functions

SAS-SY GOODNESS

BREW SOME

Page 36: SAS Input v. Put Functions

round 1 abc abc ?

Page 37: SAS Input v. Put Functions

round 1

ID_new

‘000451’

‘000934’

‘005660’

‘049712’

‘180456’

‘196245’

abc abc ?

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

Page 38: SAS Input v. Put Functions

round 1 abc abc

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

ID_new

‘000451’

‘000934’

‘005660’

‘049712’

‘180456’

‘196245’

ID_new = put(input(ID_old, 6.0), z6.0) ;

Page 39: SAS Input v. Put Functions

ID_new = put(input(ID_old, 6.0), z6.0) ;

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

char

round 1 abc abc

Page 40: SAS Input v. Put Functions

ID_new = put(input(ID_old, 6.0), z6.0) ;

(inter_num)

451

934

5660

49712

180456

196245

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

char num

round 1 abc abc

Page 41: SAS Input v. Put Functions

ID_new = put(inter_num, z6.0) ;

(inter_num)

451

934

5660

49712

180456

196245

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

char num

round 1 abc abc

Page 42: SAS Input v. Put Functions

ID_new = put(inter_num, z6.0) ;

(inter_num)

451

934

5660

49712

180456

196245

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

char num

round 1 abc abc

Page 43: SAS Input v. Put Functions

ID_new

000451

000934

005660

049712

180456

196245

(inter_num)

451

934

5660

49712

180456

196245

ID_old

‘451’

‘934’

‘5660’

‘49712’

‘180456’

‘196245’

char num char

round 1 abc abc

ID_new = put(inter_num, z6.0) ;

Page 44: SAS Input v. Put Functions

new = put(input(old, 6.0), z6.0) ;

round 1

Nest functions when going from char to char.

Don’t be afraid; break it down step-by-step.

❶ ❷

abc abc

Page 45: SAS Input v. Put Functions

round 2 custom informat: abc 123 ?

Page 46: SAS Input v. Put Functions

gpa_char

‘A-’

‘B+’

‘C’

‘B-’

‘B’

‘D’

gpa_num

‘000451’

‘000934’

‘005660’

‘049712’

‘180456’

‘196245’

round 2 custom informat: abc 123 ?

Page 47: SAS Input v. Put Functions

if gpa_char in ('A+' 'A') then gpa_num = 4.0 ;

else if gpa_char = 'A-' then gpa_num = 3.7 ;

else if gpa_char = 'B+' then gpa_num = 3.3 ;

else if gpa_char = 'B' then gpa_num = 3.0 ;

else if gpa_char = 'B-' then gpa_num = 2.7 ;

else if gpa_char = 'C+' then gpa_num = 2.3 ;

else if gpa_char = 'C' then gpa_num = 2.0 ;

else if gpa_char = 'D' then gpa_num = 1.0 ;

else if gpa_char = 'F' then gpa_num = 0 ;

else gpa_char = '' ;

round 2 custom informat: abc 123

Page 48: SAS Input v. Put Functions

proc format;

invalue gpafmt ‘A+’ = 4.0

‘A’ = 4.0

‘A-’ = 3.7

‘B+’ = 3.3

‘B’ = 3.0

‘B-’ = 2.7

‘C+’ = 2.3

‘C’ = 2.0

‘D’ = 1.0

‘F’ = 0 ;

run;

gpa_num = input(gpa_char, gpafmt.);

round 2 custom informat: abc 123

Page 49: SAS Input v. Put Functions

round 2

gpa_char

‘A-’

‘B+’

‘C’

‘B-’

‘B’

‘D’

gpa_num

3.7

3.3

2.0

2.7

3.0

1.0

custom informat: abc 123

Page 50: SAS Input v. Put Functions

gpa_num = input(gpa_char, informat.);

round 2

gpa_char

‘A-’

‘B+’

‘C’

‘B-’

‘B’

‘D’

gpa_num

3.7

3.3

2.0

2.7

3.0

1.0

custom informat: abc 123

Page 51: SAS Input v. Put Functions

round 2

gpa_char

‘A-’

‘B+’

‘C’

‘B-’

‘B’

‘D’

gpa_num

3.7

3.3

2.0

2.7

3.0

1.0

gpa_num = input(gpa_char, gpafmt.);

custom informat: abc 123

proc format;

invalue gpafmt ‘A+’ = 4.0

‘A’ = 4.0

‘A-’ = 3.7

‘B+’ = 3.3

‘B’ = 3.0

‘B-’ = 2.7

‘C+’ = 2.3

‘C’ = 2.0

‘D’ = 1.0

‘F’ = 0 ;

run;

Page 52: SAS Input v. Put Functions

new = input(old, custominfmt.) ;

round 2 custom informat: abc 123

Convert from char to num not just in type but also content.

Powerful for simplifying code.

❶ ❷

Page 53: SAS Input v. Put Functions

bonus round custom format: 123 abc ?

gpa_avg

3.50000

2.66667

2.31540

1.34500

3.95000

2.45150

...

gpa_letter

3.7

3.3

2.0

2.7

3.0

1.0

Page 54: SAS Input v. Put Functions

if 0 =< gpa_avg < 0.7 then gpa_letter = ‘F’ ;

else if 0.7 =< gpa_avg < 1.7 then gpa_letter = ‘D’ ;

else if 1.7 =< gpa_avg < 2.7 then gpa_letter = ‘C’ ;

else if 2.7 =< gpa_avg < 3.7 then gpa_letter = ‘B’ ;

else if 3.7 =< gpa_avg < 4.0 then gpa_letter = ‘A’ ;

else if gpa_avg = . then gpa_letter = ‘’ ;

bonus round custom format: 123 abc ?

Page 55: SAS Input v. Put Functions

proc format ;

value gpafmt 0 -< 0.7 = 'F'

0.7 -< 1.7 = 'D'

1.7 -< 2.7 = 'C'

2.7 -< 3.7 = 'B'

3.7 - 4.0 = 'A' ;

run ;

gpa_letter = put(gpa_avg, gpafmt.);

bonus round custom format: 123 abc

Page 56: SAS Input v. Put Functions

bonus round

gpa_avg

3.50000

2.66667

2.31540

1.34500

3.95000

2.45150

...

gpa_letter

3.7

3.3

2.0

2.7

3.0

1.0

custom format: 123 abc

Page 57: SAS Input v. Put Functions

bonus round

gpa_letter = put(gpa_avg, format.);

custom format: 123 abc

gpa_avg

3.50000

2.66667

2.31540

1.34500

3.95000

2.45150

...

gpa_letter

3.7

3.3

2.0

2.7

3.0

1.0

Page 58: SAS Input v. Put Functions

bonus round

gpa_letter = put(gpa_avg, format.);

custom format: 123 abc

gpa_avg

3.50000

2.66667

2.31540

1.34500

3.95000

2.45150

...

gpa_letter

3.7

3.3

2.0

2.7

3.0

1.0

proc format ;

value gpafmt 0 -< 0.7 = 'F'

0.7 -< 1.7 = 'D'

1.7 -< 2.7 = 'C'

2.7 -< 3.7 = 'B'

3.7 - 4.0 = 'A' ;

run ;

Page 59: SAS Input v. Put Functions

bonus round

gpa_letter = put(gpa_avg, gpafmt.);

custom format: 123 abc

gpa_avg

3.50000

2.66667

2.31540

1.34500

3.95000

2.45150

...

gpa_letter

3.7

3.3

2.0

2.7

3.0

1.0

proc format ;

value gpafmt 0 -< 0.7 = 'F'

0.7 -< 1.7 = 'D'

1.7 -< 2.7 = 'C'

2.7 -< 3.7 = 'B'

3.7 - 4.0 = 'A' ;

run ;

Page 60: SAS Input v. Put Functions

bonus round

gpa_avg

3.50000

2.66667

2.31540

1.34500

3.95000

2.45150

...

gpa_letter

‘B’

‘C’

‘C’

‘D’

‘A’

‘C’

...

gpa_letter = put(gpa_avg, gpafmt.);

custom format: 123 abc

proc format ;

value gpafmt 0 -< 0.7 = 'F'

0.7 -< 1.7 = 'D'

1.7 -< 2.7 = 'C'

2.7 -< 3.7 = 'B'

3.7 - 4.0 = 'A' ;

run ;

Page 61: SAS Input v. Put Functions

new = put(old, customfmt.) ;

Convert from num to char not just in type but also content.

Powerful for clarifying output.

❶ ❷

bonus round custom format: 123 abc

Page 62: SAS Input v. Put Functions
Page 63: SAS Input v. Put Functions

FOLD

TACK ENJOY

Page 64: SAS Input v. Put Functions

CONTACT

REFERENCES

Donna Chan

ext 8883

room 1864

[email protected]

1 – An Animated Guide: Using the Put and Input Functions | 2008

by Russ Lavery & Alejandro Jaramillo

2 – Using PROC FORMAT to Create Dynamic Lookups | 2012

by Jed Teres

3 – Five Things Every Presenter Should Know About People | 2012

by Brain Pickings

Page 65: SAS Input v. Put Functions