BookmarkSubscribeRSS Feed
JUMMY
Obsidian | Level 7

I have a data that looks like this.

 

X             XX

0              0

0              0

0              0

1              0

1              0

1              0

1              0

 

The X variable are values rounded UP to the nearest 5.  And the XX variable indicate the number of years smoked as of January 1, 1982. How do I format these variables to indicate the descriptions I gave above? Because I want to calculate a new variable using X divided by 20 and then multiply by XX. How do I do this?

7 REPLIES 7
PaigeMiller
Diamond | Level 26

@JUMMY wrote:

I have a data that looks like this.

 

X             XX

0              0

0              0

0              0

1              0

1              0

1              0

1              0

 

The X variable are values rounded UP to the nearest 5.  And the XX variable indicate the number of years smoked as of January 1, 1982. How do I format these variables to indicate the descriptions I gave above?


It's really hard to tell since your XX is all zeros, you have to give us meaningful example data for us to be able to help.

 

With regards to X, rounding up to the next unit of 5 can be done like this

 

x = round(x+2.49,5);

Because I want to calculate a new variable using X divided by 20 and then multiply by XX. How do I do this?

 

new_variable = (x/20)*xx;
--
Paige Miller
JUMMY
Obsidian | Level 7

Why x+2.49? Why add 2.49?

PaigeMiller
Diamond | Level 26

Hint: try it. What happens if x=0, what do you get? What happens if x=0.1? What do you get?

--
Paige Miller
JUMMY
Obsidian | Level 7

When X=0, I got 0 when I tried it. Unfortunately there is no x=0.1 in the data. Its all 0s and 1s. Thats why I needed help in converting the variables. Maybe some values are hidden. I am new to SAS.

Reeza
Super User

@JUMMY wrote:

When X=0, I got 0 when I tried it. Unfortunately there is no x=0.1 in the data. Its all 0s and 1s. Thats why I needed help in converting the variables. Maybe some values are hidden. I am new to SAS.


The purpose is to think about what happens. There isn't a function that allows you to always round 'up' so to accomplish that you can add a factor that will cause it to round correctly all the time. A 'normal' round would round 0.1 to 0, when you want it rounded up to 5.

 

How do I format these variables to indicate the descriptions I gave above? 

What do you mean by format? SAS has format but I suspect that's not what you're looking for here.

 

 Because I want to calculate a new variable using X divided by 20 and then multiply by XX. How do I do this?

You can calculate a new variable in a data step or PROC SQL, but it's pretty easy to do this in a single line. Again, not sure how this relates to your original post. I would suggest you post better example data and show what you want as output if you need more specific help. Additionally, the first programming course in SAS is free (search free e course SAS ) and you'll find the latest links.

 

NewVar = (X / 20) * XX;
PaigeMiller
Diamond | Level 26

@JUMMY wrote:

When X=0, I got 0 when I tried it. Unfortunately there is no x=0.1 in the data. Its all 0s and 1s. Thats why I needed help in converting the variables. Maybe some values are hidden. I am new to SAS.


You could, instead of expecting the value to be in your data, actually think about what happens if X is 0.1. It's not a hard formula to think through.

 

Alternatively, you could create phony data and see what happens is X is 0.1.

--
Paige Miller
ballardw
Super User

@JUMMY wrote:

I have a data that looks like this.

 

X             XX

0              0

0              0

0              0

1              0

1              0

1              0

1              0

 

The X variable are values rounded UP to the nearest 5.  And the XX variable indicate the number of years smoked as of January 1, 1982. How do I format these variables to indicate the descriptions I gave above? Because I want to calculate a new variable using X divided by 20 and then multiply by XX. How do I do this?


If you know how to manually calculate the result you want you should show us what that result is for some of the values.

 

I have concerns about what you might mean by " X variable are values rounded UP to the nearest 5". Since you don't show any values of 5 then your description of x seems incomplete.

 

The basic code would look something like:

data want;

    set have;

    newvalue = (x/20) * xx.

run;

where have is a data set containing your x and xx variables.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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