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

Hello,

 

I would like to find the last digit after the decimal place of the non-integer variable in SAS 9.4.

e.g. I would like the last digit of 1.48 to show up as 8

-

infile datalines flowover;
input cohort id t1_trial1 t1_trial2 t1_trial3 t2_trial1 t2_trial2 t2_trial3;
datalines;
1 30 1.6054 1.48 1.675 1.88 1.905 2.015
1 31 2.235 1.605 1.64 1.81 1.815 1.855
1 32 3.87 3.075 3.06 3.295 3.18 3.155
1 55 2.76 2.535 2.97 2.76 2.7 3.275
1 56 3.945 3.075 3.065 5.835 4.89 4.5459
1 67 2.05 1.945 1.56 3.015 3.005 3.135
1 76 1.65 2.165 1.87 2.99 2.22 2.5
;;;;
proc print; run;

/*I tried the following but no success */
data test2;
set test;

array l1 t1_trial1 - t1_trial3;
array l2 t2_trial1 - t2_trial3;
do over;
last1 = substr(l1, 1, length(l1)-1);
last2 = substr(l2, 1,length(l2)-1);
end;

run;

 

Would appreciate some help!

Margaret

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Reading your data as character is the way to do this:

data test;
input cohort id $ t1_trial1 $ t1_trial2 $ t1_trial3 $ t2_trial1 $ t2_trial2 $ t2_trial3 $;
datalines;
1 30 1.6054 1.48 1.675 1.88 1.905 2.015
1 31 2.235 1.605 1.64 1.81 1.815 1.855
1 32 3.87 3.075 3.06 3.295 3.18 3.155
1 55 2.76 2.535 2.97 2.76 2.7 3.275
1 56 3.945 3.075 3.065 5.835 4.89 4.5459
1 67 2.05 1.945 1.56 3.015 3.005 3.135
1 76 1.65 2.165 1.87 2.99 2.22 2.5
;
run;

data test2;
set test;
array trials t1_trial1 - t1_trial3 t2_trial1 - t2_trial3;
array last_digits $ last_digit1 - last_digit6;
do i = 1 to dim(trials);
 last_digits(i) = first(reverse(trim(trials(i))));
end;
run;

View solution in original post

2 REPLIES 2
SASKiwi
PROC Star

Reading your data as character is the way to do this:

data test;
input cohort id $ t1_trial1 $ t1_trial2 $ t1_trial3 $ t2_trial1 $ t2_trial2 $ t2_trial3 $;
datalines;
1 30 1.6054 1.48 1.675 1.88 1.905 2.015
1 31 2.235 1.605 1.64 1.81 1.815 1.855
1 32 3.87 3.075 3.06 3.295 3.18 3.155
1 55 2.76 2.535 2.97 2.76 2.7 3.275
1 56 3.945 3.075 3.065 5.835 4.89 4.5459
1 67 2.05 1.945 1.56 3.015 3.005 3.135
1 76 1.65 2.165 1.87 2.99 2.22 2.5
;
run;

data test2;
set test;
array trials t1_trial1 - t1_trial3 t2_trial1 - t2_trial3;
array last_digits $ last_digit1 - last_digit6;
do i = 1 to dim(trials);
 last_digits(i) = first(reverse(trim(trials(i))));
end;
run;
urban58
Quartz | Level 8
Thank you so much for the speedy reply, it worked great!
Much appreciated

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
  • 2 replies
  • 691 views
  • 0 likes
  • 2 in conversation