DATA Step, Macro, Functions and more

How to convert 5ft.4in.(Char) into inches(Num);

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

How to convert 5ft.4in.(Char) into inches(Num);

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


Accepted Solutions
Solution
‎07-02-2011 05:37 PM
Super Contributor
Posts: 408

How to convert 5ft.4in.(Char) into inches(Num);

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.

View solution in original post


All Replies
Solution
‎07-02-2011 05:37 PM
Super Contributor
Posts: 408

How to convert 5ft.4in.(Char) into inches(Num);

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.

Contributor
Posts: 35

How to convert 5ft.4in.(Char) into inches(Num);

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

Valued Guide
Posts: 2,174

How to convert 5ft.4in.(Char) into inches(Num);

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

Contributor
Posts: 35

How to convert 5ft.4in.(Char) into inches(Num);

Thanks for the Info.

Super Contributor
Posts: 408

How to convert 5ft.4in.(Char) into inches(Num);

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.

Contributor
Posts: 35

How to convert 5ft.4in.(Char) into inches(Num);

Thanks. Sure I will google it.

Super User
Posts: 9,671

How to convert 5ft.4in.(Char) into inches(Num);

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

Contributor
Posts: 35

How to convert 5ft.4in.(Char) into inches(Num);

Thanks for the solution.

Learner
Posts: 1

Re: How to convert 5ft.4in.(Char) into inches(Num);

Hi. What does 'kd' and best stands for in your code. thanks

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 1973 views
  • 4 likes
  • 5 in conversation