error · ta à numeric ulations. now that i know what format i want, how do make it happen?...

33

Upload: danghanh

Post on 05-Jul-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise
Page 2: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise
Page 3: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

ERROR:

ERROR:

ERROR:

ERROR:

ERROR:

Page 4: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Formatting Variables: Back and forth between character and numeric Back and forth between character and numeric

Formatting Variables: Back and forth between character and numeric Back and forth between character and numeric

Page 5: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Why should you care? DATA name1; SET name; if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Why should you care?

if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Page 6: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Why should you care? DATA name1; SET name; if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Why should you care?

if var = ‘Three’ then delete;; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Page 7: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Why should you care? DATA name1; SET name; if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Why should you care?

if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Page 8: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Why should you care? DATA name1; SET name; if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Why should you care?

if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

Page 9: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Why should you care? DATA name1; SET name; if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

OK

Why should you care?

if var = ‘Three’ then delete; if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

OK

Page 10: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Why should you care? DATA name1; SET name; if var = ‘Three’ if var = ‘3’ then delete; if var = ‘3’ then delete; if var = 3 then delete;

RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

OK

Why should you care?

’ then delete; then delete; then delete;

if var = 3 then delete;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

OK

Page 11: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

PROBLEM: Identify the same variable as character and numeric

SAS attempts to make them comparable § Character ßà Numeric

“Implicit” coding “Implicit” coding

Should avoid § SAS is making the decisions § Opportunity to introduce errors § FRUSTRATION!

PROBLEM: Identify the same variable as character and

SAS attempts to make them comparable

SAS is making the decisions – not you Opportunity to introduce errors

Page 12: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Know the current format of your data.

PROC CONTENTS; RUN; PROC CONTENTS; RUN;

Know the current format of your data.

PROC CONTENTS; RUN; PROC CONTENTS; RUN;

Page 13: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise
Page 14: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

SAS Enterprise Guide SAS Enterprise Guide

Page 15: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

CHAR CHAR NUM

Regular SAS CHAR CHAR

Page 16: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

How do I decide what format my variable should be?

How do I decide what format my variable should be?

Page 17: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Selecting the best format

Obvious choices § if a variable contains non-numeric information § If a variable contains numeric data

Not so obvious choices Not so obvious choices § Integer data that won’t be used in any calculations § ID Number = 1757638

§ Nominal variables § Sex: 1 = Male, 2 = Female

Use numeric variables whenever possible § Eliminates leading and trailing blanks

Selecting the best format

numeric information à character If a variable contains numeric data à numeric

Integer data that won’t be used in any calculations

Use numeric variables whenever possible Eliminates leading and trailing blanks

Page 18: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Now that I know what format I want, how do make it happen? want, how do make it happen? Now that I know what format I want, how do make it happen? want, how do make it happen?

Page 19: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

INFORMATS

FORMATS FORMATS

INPUT & PUT statements

INFORMATS

FORMATS FORMATS

INPUT & PUT statements

Page 20: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Non-SAS input file

SAS dataset

INFORMAT

Tells SAS how to read data from external files into SAS variables

input file dataset

INPUT statement PUT statement

SAS dataset

Output

FORMAT

Instructions for outputting data

Changes how the variable is presented but does not change the variable itself

dataset

INPUT statement PUT statement

Page 21: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

INFORMAT DATA name; Infile ‘C:/My Files/Jacq’;

informat VAR1 $8.; informat VAR2 4.1; informat VAR2 4.1; input VAR1 $

VAR2 ;

RUN;

Infile ‘C:/My Files/Jacq’; informat VAR1 $8.; informat VAR2 4.1; informat VAR2 4.1;

Page 22: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Numeric examples § 4.1 § COMMA10. § HEX4.

Character examples Character examples § $CHAR10. § $10. § $HEX4.

Page 23: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

FORMAT PROC FORMAT; value gender 1

2 RUN; RUN;

PROC PRINT; VAR var_name; FORMAT var_name gender. RUN;

= ‘Male’ = ‘Female’;

gender.;

Page 24: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

FORMAT PROC FORMAT; value gender 1 = ‘Male’

2 = ‘Female’; RUN; RUN;

PROC PRINT; VAR var_name; FORMAT var_name gender. RUN;

1 = ‘Male’ 2 = ‘Female’;

gender.;

Page 25: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

FORMAT PROC FORMAT; value $gender ‘M’ = ‘Male’

‘F’ = ‘Female’; RUN; RUN;

PROC PRINT; VAR var_name; FORMAT var_name $gender.; RUN;

‘M’ = ‘Male’ ‘F’ = ‘Female’;

FORMAT var_name $gender.;

Page 26: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

FORMAT PROC FORMAT; value $gender ‘M’ = ‘Male’

‘F’ = ‘Female’; RUN; RUN;

PROC PRINT; VAR var_name; FORMAT var_name $ RUN;

‘M’ = ‘Male’ ‘F’ = ‘Female’;

$gender.;

Page 27: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

INPUT & PUT Statements INPUT - converts character to numeric

PUT - converts numeric to character

Not possible to directly change the type of variable § Must create new variable § Rename § Drop

INPUT & PUT Statements converts character to numeric

converts numeric to character

Not possible to directly change the type of

Must create new variable

Page 28: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

DATA data1; SET data;

/*Character to numeric */ new_num=INPUT(char_var,$4);

/* Numeric to character */ new_char=PUT(num_var,4.0);

RUN;

Character to numeric */ new_num=INPUT(char_var,$4);

/* Numeric to character */ new_char=PUT(num_var,4.0);

Page 29: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

In sum…. In sum….

Page 30: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

What format should you use when the choice is not obvious?

NUMERIC NUMERIC

Why?

Don’t have to worry about leading or trailing blanks

What format should you use when the choice is

Don’t have to worry about leading or trailing

Page 31: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Know the current format of your data.

1. PROC CONTENTS 2. Enterprise Guide SAS 2. Enterprise Guide SAS

- Numeric - blue circles - Character - red triangles

3. REGULAR SAS - Numeric – right justified - Character – left justified

Know the current format of your data.

Enterprise Guide SAS Enterprise Guide SAS blue circles

red triangles

right justified left justified

Page 32: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

Non-SAS input file

SAS dataset

INFORMAT

? ?

input file dataset

INPUT statement PUT statement

? SAS

dataset Output

FORMAT

? ?

dataset

INPUT statement PUT statement

?

Page 33: ERROR · ta à numeric ulations. Now that I know what format I want, how do make it happen? INFORMATS FORMATS ... Know the current format of your data. 1. PROC CONTENTS 2. Enterprise

NOTE: The data set COPD.MASTER has

14,295 observations and 23 variables

NOTE: The data set COPD.MASTER has

14,295 observations and 23 variables