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

 

Hi. I need some help. I saw another ? that was similar, but it did not work.  My goal is to convert a text variable, such as 6'4" into inches (then I can change it to the metric system).

 

The data looks like this - it's a text var. 

 

ht

5'  6"

5'11"

6' 2"

etc.   they have different spacings, too. 

 

If anyone could help me, please do so.  There are different widths between the feet and inches as well. 

Thanks. Mava

 

1 ACCEPTED SOLUTION

Accepted Solutions
mgrzyb
Quartz | Level 8

Hey!

 

I GOT IT!!!!!

 

Yes!  Thank you all!!!! 

 

I figured it out.  Just had to step away from the computer for a while.  

View solution in original post

12 REPLIES 12
satya7777
Fluorite | Level 6

Please try the following code. It may meet your requirement.

 

data test;
input ht $10.;
datalines;
5'6"
5'11"
6'2"
;
run;

data test2(drop=t1);
set test;
feet=input(compress(scan(ht,1,"'")),3.);
t1=index(ht,"'");
inches=input(compress(scan(substr(ht,t1+1),1,'"')),3.);
run;

 

The output contains feet and inches values in separate variables. Perform numeric operation and add those values as per your requirement.

 

Thanks & Regards,

Satya

mgrzyb
Quartz | Level 8

Satya,

 

Thank you! It works. But it's in a permanent dataset.  

 

So how do I handle that?  I have tons of variables and don't know the spacing. 

 

I would like to know how to handle that for a huge database.

 

 

This works when I copy the data for a separate database = I can always merge it by ID, but there has to be a way to do this using a permanent database. 

 

Any suggestions? 

mgrzyb
Quartz | Level 8

This is the code I have - it's a permanent database. 

Sayta's pgm helped, but I can't figure it out for a perm. database.  It doesn't work unless I copy thousands of ID vars and ht variable. 

My variable for ht is named ht2mg.

 

I tried this:


data a;
set aung.try_ht2;
run;

data b(drop=ht2mg);
set a;
feet=input(compress(scan(ht2mg,1,"'")),3.);
ht2mg=index(ht2mg,"'");
inches=input(compress(scan(substr(ht2mg,t1+1),1,'"')),3.);
run;

proc print data=b; run;

andreas_lds
Jade | Level 19

Sorry, can't understand your problem.

 

You have a dataset named aung.try_ht2 having a variable named ht2mg, containing something in feet and inches (the example data you posted).  The code you posted should give you the required result. What do you want as output?

 

EDIT:

Now i see, you made a small change to the code provided by @satya7777, you have replaced "t1" with "ht2mg". The log should contain some notes about missing values.

data b(drop=ht2mg);
  set aung.try_ht2;
  feet=input(compress(scan(ht2mg,1,"'")),3.);
  t1=index(ht2mg,"'"); /* was: ht2mg = index.... */
  inches=input(compress(scan(substr(ht2mg,t1+1),1,'"')),3.);
run;

 

 

Another way to extract numbers from strings is using regular expressions:

data want;
   set test;

   length feet inches 8 rx 8;
   retain rx;

   if _n_ = 1 then do;
      rx = prxparse(cats("/^(\d+)'", '\s*(\d+)"\s*/'));
   end;
   
   if prxmatch(rx, trim(ht)) then do;
      feet = input(prxposn(rx, 1, trim(ht)), best.);
      inches = input(prxposn(rx, 2, trim(ht)), best.);;
   end;

run;
mgrzyb
Quartz | Level 8

the feet in a number and inches in a number.  I got errors.  So I tried this:

 

 

ODS HTML close; ODS listing ;
data a (keep= id mrn ht2mg ) out=b;
set aung.try_ht2;
run;
proc print data=b noobs; run;

 

data c (drop=t1);
set b;

feet=input(compress(scan(ht2mg,1,"'")),3.);
t1=index(ht2mg,"'");
inches=(compress(scan(substr(ht2mg,t1+1),1,'"')),3.);
run;

proc print data=c; run;

 

 

I don't understand what the t1 means.   I do thank you for your code, but I have thousands to do.  I can separate the id and mrn with ht2mg (text var) into a separate database, but is there an easier way?  The code above did not work. 

 

 I got these errors:

 

 


250
251
252 data c (drop=t1);
253 set b;
254
255 feet=input(compress(scan(ht2mg,1,"'")),3.);
256 t1=index(ht2mg,"'");
257 inches=(compress(scan(substr(ht2mg,t1+1),1,'"')),3.);
-
22
200
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, ), *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,
IN, LE, LT, MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.

ERROR 200-322: The symbol is not recognized and will be ignored.

258 run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
257:9
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.C may be incomplete. When this step was stopped there were 0 observations and 5 variables.
WARNING: Data set WORK.C was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds

 

 

thank you. 

mgrzyb
Quartz | Level 8

 

 

ODS HTML close; ODS listing ;
data a (keep= id mrn ht2mg) ;
set aung.try_ht2;
run;

 


data b (drop=t1);
set a;

feet=input(compress(scan(ht2mg,1,"'")),3.);
t1=index(ht2mg,"'");
inches=(compress(scan(substr(ht2mg,t1+1),1,'"')),3.);
run;

 

proc print data=b; run;

 

 

 

My log says this:

 


326
327 data b (drop=t1);
328 set a;
329
330 feet=input(compress(scan(ht2mg,1,"'")),3.);
331 t1=index(ht2mg,"'");
332 inches=(compress(scan(substr(ht2mg,t1+1),1,'"')),3.);
-
22
200
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, ), *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,
IN, LE, LT, MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.

ERROR 200-322: The symbol is not recognized and will be ignored.

333 run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
332:9
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.B may be incomplete. When this step was stopped there were 0 observations and 5 variables.
WARNING: Data set WORK.B was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds

 

 

andreas_lds
Jade | Level 19

"t1" is just a variable containing the position of the first ' in ht2mg.

 

Please post code and log always using {i} or running man icon, to maintain readability.

 

Still confusing: "I do thank you for your code, but I have thousands to do" - hardly able to understand what you mean.

 

Can you post an extract of your data as data-step, so that we can see what you really have?

mgrzyb
Quartz | Level 8

Sure …. give a minute to get rid of the personal information.  

 

When I said, "thanks," I meant that it worked if I just typed out those heights.  I have a huge dataset of about 2 thousand people.  I don't have the time to retype it all for an input statement.  

 

Thanks.

mgrzyb
Quartz | Level 8

I have attached an abbreviated perm data set which includes ht2mg.  In the big data set, I have many more variables not in this order, but I can reorder it.  

 

Thanks.  I can't seem to attach the perm. data set. It won't accept it.     I tried copying it in word, a sas perm data set, but the attachment part will not allow me to attach it!!

 

 

mgrzyb
Quartz | Level 8

Here is my code:

 


data a;
set aung.partial_dset_mg ;
run;


data test2(drop=t1);
set a;
feet=input(compress(scan(ht,1,"'")),3.);
t1=index(ht,"'");
inches=input(compress(scan(substr(ht,t1+1),1,'"')),3.);
run;


proc print data=test2;
run;

 

 

Please help me.  I need this for future use as well. Thanks. 

mgrzyb
Quartz | Level 8

Hey!

 

I GOT IT!!!!!

 

Yes!  Thank you all!!!! 

 

I figured it out.  Just had to step away from the computer for a while.  

ChrisNZ
Tourmaline | Level 20

Please accept the post that helped you as the solution rather than your own "it works now" post.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 12 replies
  • 1291 views
  • 0 likes
  • 4 in conversation