Hello,
I am working with a secondary dataset that includes a rather complicated variable where some levels have many trailing zeros. For some reason, not all levels are included when I create a new variable based on the original variable levels. For example, one of the levels for the variable is 1020405100000 (n=16). But when I try to test the creation of a new variable based just on this value using the code below, it does not display the appropriate output of 16. Rather it says "level=0." This pattern continues for levels for this variable that include trailing zeros. However, if I insert shorter levels of the variable such as 1110 (n=20) in the below code, the new variable accurately reflects n=20.
if q12=1020405100000 then newvar=1;
It was suggested that I attempt to truncate the variable to see this would solve the problem.
So instead of having:
1 1020405100000
2 10304051000000000
....;
It would be:
1 102040510
2 103040510
...;
Do you think this would solve the issue? If so, how is the best way to truncate the variable and remove the trailing zeros?
Thank you for any help.
SUBSTRN(PUT(num_val, 20.), -1, 11)
Have you tried SUBSTRN function yet?
Are you sure the variable is a number and not a character string?
Anyway, you cannot store more than 15 or so decimal places in a number in SAS. SAS stores all numbers as floating point. You can use CONSTANT() function to see what is the largest integer that can be stored exactly.
174 data _null_; 175 max=constant('exactint'); 176 put max= comma32.; 177 run; max=9,007,199,254,740,992
So convert it to character. Or split it into two or more numbers.
@sfw_09 wrote:
Hello,
I am working with a secondary dataset that includes a rather complicated variable where some levels have many trailing zeros. For some reason, not all levels are included when I create a new variable based on the original variable levels. For example, one of the levels for the variable is 1020405100000 (n=16). But when I try to test the creation of a new variable based just on this value using the code below, it does not display the appropriate output of 16. Rather it says "level=0." This pattern continues for levels for this variable that include trailing zeros. However, if I insert shorter levels of the variable such as 1110 (n=20) in the below code, the new variable accurately reflects n=20.
if q12=1020405100000 then newvar=1;
It was suggested that I attempt to truncate the variable to see this would solve the problem.
So instead of having:
1 1020405100000
2 10304051000000000
....;
It would be:
1 102040510
2 103040510
...;
Do you think this would solve the issue? If so, how is the best way to truncate the variable and remove the trailing zeros?
Thank you for any help.
What you show sounds much like string manipulation has been done somewhere along the line. Note that for many purposes a character value will default to 8 characters, which is what you show. And if the value were actually numeric that comparison would yield newvar = 1.
Proof:
data example; q12 = 1020405100000 ; if q12=1020405100000 then newvar=1; run;
So show us the result for proc contents for the variable Q12. I will bet a short stack of $$ that it will show character with length 8.
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!
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.