Posted by : Netbloggy Saturday, September 19, 2015

Like various numeric functions that help us to process numeric data, SAS has a bunch of Character Functions to easily process Characters.

Problem:

Clean the input data where Weight & Height are characters but remove
the units after the numbers & type convert them to numbers

Solution:

/* Clean the input data where Weight & Height are characters but remove 
the units after the numbers & type convert them to numbers */
 
data A15001.A01_nuts;
	input weight $ height $;
	datalines;
	100kgs. 59cm
	180KG 60CM.
	88kg. 160cms
	50kgs 100cm
	;
 
Title 'Source Data';	
proc print data=A15001.A01_nuts; run;Title;
* find() to find 'kg' and 'cm' in the input set
returns the position of the matched string;
 
data A15001.A01_fixit;
	set A15001.A01_nuts(rename = (weight = char_wt
						height = char_ht));				
	if find(char_wt,'kg','i') then	
			weight = input(compress(char_wt,,'kd'),8.);
	if find(char_ht,'cm','i') then	
			height= input(compress(char_ht,,'kd'),8.);
	drop char_:; 
run;
 
Title 'Formatted Data';
proc print data=A15001.A01_fixit; run;Title;

Output:
Learning: 


  1. Understanding Basic character functions 
  2. Converting Character variable to Numeric variable using input (put for the opposite of it)

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Blogger templates

Total Pageviews

Powered by Blogger.

- Copyright © nulldata -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -