I have a variable that is all numbers but in character string, because I extracted the numbers from another variable. For example, the variable 'a' is like:
a
1,2
2,3,4
And I created a new variable 'b' by extracting numbers from 'a', like below:
b
1
2
2
3
4
But the new 'b' variable is character string, I tried to convert it to numeric by using:
b_n=input(b,8.);
However, the b_n is all missing values (bunch of '.'s). What's going on with this situation? Could anyone help me? Thank you!
data long;
set one;
do i =1 to countw(a, ',');
b=scan(a,i, ',');
b_n=input(trim(b), 8.);
output;
end;
run;
Order of operations issue. You are outputting the data before b_n is calculated and only calculating it for the last value.
Show your code?
EDIT: add a trim to remove any additional white space?
b_n=input(trim(b), 8.);
I tried, still not working, I used the code like this:
data long;
set one;
do i =1 to countw(a, ',');
b=scan(a,i, ',');
output;
end;
b_n=input(trim(b), 8.);
run;
This is the second time that I encountered this. Last time I remember that I extracted data from excel, and it always produced missing values, so I exported to excel and changed the format and imported back.
data long;
set one;
do i =1 to countw(a, ',');
b=scan(a,i, ',');
b_n=input(trim(b), 8.);
output;
end;
run;
Order of operations issue. You are outputting the data before b_n is calculated and only calculating it for the last value.
You really need to show the code of how you get from A to B.
If the purpose is get the numeric value from A into a new variable then don't bother creating B.
data have; input a $; datalines; 1,2 2,3,4 ; data want; set have; do i=1 to countw(a,','); bn=input(scan(a,i,','),8.); output; end; drop i; run;
Why are you telling INPUT() to read only the first 8 bytes of B? Are you sure B isn't already a number? If it is then SAS will first convert to a string using BEST12. format and if you only read the first 8 bytes they will all be blanks. But if it is already a number then there is no need for the INPUT() function call.
If it is a string then again why are you only trying to read the first 8 bytes? The normal numeric function can ready 32 bytes. And INPUT() does not care if the string is shorter than the width on the informat. But you might want to remove any leading spaces.
b_n=input(left(b),32.);
@SAS-questioner wrote:
... stuff deleted ...
it still didn't work.
Simply telling us that something "doesn't work" ... doesn't work.
Show us the code and the log, and describe what you got vs what you expected. Help us help you.
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.