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:
However, the b_n is all missing values (bunch of '.'s). What's going on with this situation? Could anyone help me? Thank you!
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.
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.
... 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.
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.