BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
john83
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

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.

View solution in original post

9 REPLIES 9
jklaverstijn
Rhodochrosite | Level 12

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.

john83
Calcite | Level 5

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

Peter_C
Rhodochrosite | Level 12

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

john83
Calcite | Level 5

Thanks for the Info.

jklaverstijn
Rhodochrosite | Level 12

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.

john83
Calcite | Level 5

Thanks. Sure I will google it.

Ksharp
Super User
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

john83
Calcite | Level 5

Thanks for the solution.

id1887
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 7323 views
  • 6 likes
  • 5 in conversation