Hi Guys,
I have a dataset which consists of Height variable. Data type of height is Char.sample data values of Height var. are 5ft. 4in. , 6ft. 10in.
I have to convert these char values into numeric values. final output : the datavalues should be in inches.
Could you please help me regarding this problem.
Thanks in advance.
John
Short of moving to a country that has a decent metric system in place, I suggest using regular a expression. This shoud hopefully get you started.
data _null_;
if _n_ = 1 then do;
re = prxparse('/(\d*)ft. (\d*)in./');
end;
retain re;
input ;
if prxmatch(re, _infile_) then do;
feet = prxposn(re, 1, _infile_);
inches = prxposn(re, 2, _infile_);
end;
put _all_;
cards;
5ft. 4in.
6ft. 10in.
;
I leave it as an excercise for the reader to convert from num to char and from feet to inches.
Regards, Jan.
Editor's note: Challenge accepted. See Convert a text-based measurement to a number in SAS.
Short of moving to a country that has a decent metric system in place, I suggest using regular a expression. This shoud hopefully get you started.
data _null_;
if _n_ = 1 then do;
re = prxparse('/(\d*)ft. (\d*)in./');
end;
retain re;
input ;
if prxmatch(re, _infile_) then do;
feet = prxposn(re, 1, _infile_);
inches = prxposn(re, 2, _infile_);
end;
put _all_;
cards;
5ft. 4in.
6ft. 10in.
;
I leave it as an excercise for the reader to convert from num to char and from feet to inches.
Regards, Jan.
Editor's note: Challenge accepted. See Convert a text-based measurement to a number in SAS.
Thanks for your help.
If you have time, could you please tell me , what are prxparse,prxposn and prxmatch functions.
Your help will be greatly appreciated.
Thanks in advance,
John
to find out about a function type it and press F1 key (while the cursor is at the function name)
But do this in a SAS editor.
SAS editors provide context sensitive help
Thanks for the Info.
The context help is certainly useful. These PRX functions are part of a grander topic called the Perl regular expressions (hence the name). The SAS support site provides the much needed background at http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002288677.htm. There are some very useful SAS Forum papers as well.
Note that regular expressions (regexp's) are not invented by SAS; a Google search will illustrate this. So there is are lots of resources outside the SAS realm that can be helpful. I must admit regexp's are an acquired taste, but their power is certainly worth the effort. And who hasn't occasionally used the mother of all regexps: "*.*"?
- Jan.
Thanks. Sure I will google it.
data want; input height $20.; num_height=input(scan(height,1,,'kd'),best8.)*12+input(scan(height,2,,'kd'),best8.); cards; 5ft. 4in. 6ft. 10in. ;run;
Ksharp
Thanks for the solution.
Hi. What does 'kd' and best stands for in your code. thanks
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.