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

Hi,

 

I have a numerical variable BA_car which has 40 observations. The observations are all in decimals e.g. BA_car = 39.32. I want to split the numbers 39.32 under two new variable names (months, weeks) so that the new variable months = 39 and weeks =32. I was trying my code below for months first but it did not work. Any suggestions?

 

data cars;

set Theresa.cars;

if BA_car = . then months = .;

else if BA_car = months.weeks then months = months;

run;

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

You don't have to convert the number to a string, you could use some basic operations:

 

month = int(BA_Car);
week = int((BA_Car - month) * 100);

View solution in original post

9 REPLIES 9
andreas_lds
Jade | Level 19

This is a very interesting way to store months and weeks ...

 

You have to convert the number to string, then use scan function with . as separator to extract the parts left and right of the dot.

andreas_lds
Jade | Level 19

You don't have to convert the number to a string, you could use some basic operations:

 

month = int(BA_Car);
week = int((BA_Car - month) * 100);
PeterClemmensen
Tourmaline | Level 20

@andreas_lds, I like the idea of not converting here, but what if BA_car=39.3 ?

andreas_lds
Jade | Level 19

@PeterClemmensen wrote:

@andreas_lds, I like the idea of not converting here, but what if BA_car=39.3 ?


Well ... the result will be wrong if the "basic-ops" solution is used ... i really should have a cup of coffee before posting  😉

catch18
Obsidian | Level 7

 

Many thanks andreas_ids.

SASKiwi
PROC Star

Something like this?

 

data cars;
set Theresa.cars;
if BA_car = . then months = .;
else do;
  months = int(BA_car);
  weeks = mod(BA_car, 1) * 100;
end;
run;
catch18
Obsidian | Level 7

Thank you.

SASKiwi
PROC Star

This might be better for the 39.3 case:

 

data _null_;
BA_car = 39.3;
  months = int(BA_car);
  weeks = mod(BA_car, 1);
  if mod(weeks * 100, 10) = 0 then weeks = weeks * 10;
  else weeks = weeks * 100;
  put _all_;
 run; 

 
ballardw
Super User

@catch18 wrote:

Hi,

 

I have a numerical variable BA_car which has 40 observations. The observations are all in decimals e.g. BA_car = 39.32. I want to split the numbers 39.32 under two new variable names (months, weeks) so that the new variable months = 39 and weeks =32. I was trying my code below for months first but it did not work. Any suggestions?

 

data cars;

set Theresa.cars;

if BA_car = . then months = .;

else if BA_car = months.weeks then months = months;

run;

 

Thanks.


Don't read such variables as numeric as .3 and .31 differ buy more than ".01" if I understand you description. 39.3 would seem to be "3 weeks". Or is your data going to have 3 weeks as .03? You have provided an incomplete description of your values.

 

Read as character an split using Input and Scan functions.

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 3657 views
  • 2 likes
  • 5 in conversation