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

Hello,

 

So I have two variables in my dataset, pounds and Ounces. I am trying to combine these variables into a total weight variable.

 

For example, if an observation has a recorded 8 pounds and a recorded 2 ounces, I want to create this new variable to say 8.2. Is this possible in SAS, and if so, how do I accomplish this?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Yes it's possible. But do you really want 8.2 as 8 lbs and 2 ounces? Then you're deviating from the standard definition of the decimal value which is problematic. 

 

If you really want that, try CATX().

 

new_var = catx(".", put(pound, 8. -l), put(ounces, 8. -l));

A better approach is to store everything in one unit, ounces. 

 

weight_ounce_total = pound*16 + ounce;

@SM8 wrote:

Hello,

 

So I have two variables in my dataset, pounds and Ounces. I am trying to combine these variables into a total weight variable.

 

For example, if an observation has a recorded 8 pounds and a recorded 2 ounces, I want to create this new variable to say 8.2. Is this possible in SAS, and if so, how do I accomplish this?

 

 


 

View solution in original post

3 REPLIES 3
ballardw
Super User

@SM8 wrote:

Hello,

 

So I have two variables in my dataset, pounds and Ounces. I am trying to combine these variables into a total weight variable.

 

For example, if an observation has a recorded 8 pounds and a recorded 2 ounces, I want to create this new variable to say 8.2. Is this possible in SAS, and if so, how do I accomplish this?

 

 


Be very careful of that sort of non-standard representation. If you want a decimal version you would be better off where 2 ounces is represented as 0.125 then you actually have unit for the variable in pounds:

 

data want;

    set have;

    newweight = pounds + ounces/16;

run;

 

Would be the way with a data step to convert ounces to pounds.

 

You would have to very clearly describe the actual advantage or use of storing the value as 8.2. You couldn't do substraction, such as determining change in weight, or look up with standard tables a value as you propose.

 

Or perhaps change the whole value to ounces? Newweight = pounds*16 + ounces;

 

I dealt with some data that was provided in a form similar to what you propose only the "decimal" portion was fourths of a pound. And none of the uses wanted that sort of units so had to be converted to actual pounds before anything could be done.

Reeza
Super User

Yes it's possible. But do you really want 8.2 as 8 lbs and 2 ounces? Then you're deviating from the standard definition of the decimal value which is problematic. 

 

If you really want that, try CATX().

 

new_var = catx(".", put(pound, 8. -l), put(ounces, 8. -l));

A better approach is to store everything in one unit, ounces. 

 

weight_ounce_total = pound*16 + ounce;

@SM8 wrote:

Hello,

 

So I have two variables in my dataset, pounds and Ounces. I am trying to combine these variables into a total weight variable.

 

For example, if an observation has a recorded 8 pounds and a recorded 2 ounces, I want to create this new variable to say 8.2. Is this possible in SAS, and if so, how do I accomplish this?

 

 


 

Patrick
Opal | Level 21

@SM8 

I'd convert this data asap into a metric unit (kg) stored in a single variable to allow for any sort of calculation and aggregation.

If you must print pounds and ounces in reports then consider creating a format for it (you'd likely would need Proc FCMP and then create a format which calls the function).

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1354 views
  • 4 likes
  • 4 in conversation