DATA Step, Macro, Functions and more

Char to Num

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

Char to Num

I'm trying to change "education" to numeric. Tried all different ways, hence code in comments.

 

PROC FREQ DATA=XYZ1; TABLES EDUCATION; RUN;

 

The FREQ Procedure

education

education

Frequency

Percent

Cumulative
Frequency

Cumulative
Percent

college grad

22

17.46

22

17.46

grade or less

6

4.76

28

22.22

some college

32

25.40

60

47.62

some high or tech school;high or tech school graduate

66

52.38

126

100.00

Frequency Missing = 3

 

PROC FORMAT;

VALUE $EDU

/*

1="grade or less"

2="some high or tech school;high or tech school graduate"

 

3="some college"

4="college grad";

*/

"grade or less" = 1

"some high or tech school;high or tech school graduate" =2

"some college"=3

"college grad"=4

;

RUN;

DATA XYZ2; SET XYZ1; FORMAT EDUCATION EDU. ;

*EDUCATION=INPUT(EDUCATION, 8.);

RUN;

 

DATA XYZ3; SET XYZ2; EDUCATION1=INPUT(EDUCATION, 8.);

RUN;

 

 The note is:

NOTE: Invalid argument to function INPUT at line 166 column 35.

 

Seems like such a simple thing, but I can't get it Smiley Sad


Accepted Solutions
Solution
‎04-03-2018 01:12 PM
Super User
Posts: 13,283

Re: Char to Num

[ Edited ]

Try:

PROC FORMAT;

inVALUE EDU      /*INVALUE makes an informat to use with INPUT*/
"grade or less" = 1
"some high or tech school;high or tech school graduate" =2
"some college"=3
"college grad"=4
;
run;
DATA XYZ3; 
   SET XYZ2; 
   EDUCATION1=INPUT(EDUCATION, EDU.);

RUN;

Input statement is going to require an informat, custom ones are made with the INVALUE in proc format.

 

Note that spelling must match exactly including leading spaces and such. Case differences can be addressed with UPCASE option and the values as upper case.

Also to create a numeric value the Invalue does not start with $. You would use that create a different text value from existing text.

And last, the INPUT statement uses the Informat you created to read the text.

View solution in original post


All Replies
Contributor
Posts: 34

Re: Char to Num

length g 3;
g=33;
p=put(g, 3.);
Contributor
Posts: 34

Re: Char to Num

Oh, sorry you wanted to go the other way:

 

  length g $3;
  g='33';
  p=input(g, 3.);
Solution
‎04-03-2018 01:12 PM
Super User
Posts: 13,283

Re: Char to Num

[ Edited ]

Try:

PROC FORMAT;

inVALUE EDU      /*INVALUE makes an informat to use with INPUT*/
"grade or less" = 1
"some high or tech school;high or tech school graduate" =2
"some college"=3
"college grad"=4
;
run;
DATA XYZ3; 
   SET XYZ2; 
   EDUCATION1=INPUT(EDUCATION, EDU.);

RUN;

Input statement is going to require an informat, custom ones are made with the INVALUE in proc format.

 

Note that spelling must match exactly including leading spaces and such. Case differences can be addressed with UPCASE option and the values as upper case.

Also to create a numeric value the Invalue does not start with $. You would use that create a different text value from existing text.

And last, the INPUT statement uses the Informat you created to read the text.

Contributor
Posts: 44

Re: Char to Num

Thank you...this worked AND I learnt something new !

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 113 views
  • 1 like
  • 3 in conversation