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.
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);
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.
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);
@andreas_lds, I like the idea of not converting here, but what if BA_car=39.3 ?
@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 😉
Many thanks andreas_ids.
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;
Thank you.
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;
@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.
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.
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.
Ready to level-up your skills? Choose your own adventure.