I have a data with commas in tab file and I have imported it the values were imported into sas as a char datatype with a comma values.
like 23,1 53,2
I want to now convert these into numeric with either . or comma how do i do it?
if I use
want=input(have,comma.);
informat want comma.;
format want comma.;
I get missing values., !
You can use the NUMXw.d informat to input numbers with commas as the decimal separator.
want = input(have,NUM4.1);
or just use that on the initial input statement and you don't have to convert it.
NUMXw.d also is a format, so you can use it to display the variable with a comma if that's how you are more comfortable viewing decimals.
You can use a TRANWRD function to replace the comma with a period, then wrap this within an INPUT function to convert the new character value to numeric.
F2 = INPUT(TRANWRD(F1,',','.'),4.1);
Related
I have a set of variables in SAS that should be numeric but are characters. Numbers are comma separated and I need a point. For example, I need 19,000417537 to be 19.000417537. I tried translate without success. the comma is still there and I'm not able to convert the variable to numeric using input(). Can anyone help me please?
Thank you in advance
Best
Use INPUT() with the COMMAX informat.
data have;
length have $20.;
have = "19,000417537";
want = input(have, commax32.);
format want 32.8;
run;
proc print data=have;
run;
Obs have want
1 19,000417537 19.00041754
In two steps you can replace the , with . with tranwrd and then use input to convert it to numeric.
data yourdf;
set df;
charnum2=tranwrd(charnum, ",", "."); /*replace , with .*/
numvar = input(charnum2, 12.); /*convert to numeric*/
run;
You can use the COMMA informat to read strings with commas in them. But if you want it to treat the commas as decimal points instead of ignoring them then you probably need to use COMMAX instead (Or perhaps use the NLNUM informat instead so that the meaning of commas and periods in the text will depending on your LOCALE settings).
So if the current dataset is named HAVE and the text you want to convert is in the variable named STRING you can create a new dataset named WANT with a new numeric variable named NUMBER with code like this:
data want;
set have;
number = input(string,commax32.);
run;
I have a SAS variable(a table column) which has a Number data type and a format YYMMDD10.
It has a value like :04/20/2019
I want to convert it to a string like:20190420.
So I do:
new_date= Put( trim(orig_date), mmddyy10.);
I get:The format $MMDDYY was not found or could not be loaded.
Your main mistake is adding the unneeded TRIM() function.
You cannot use TRIM() function on a numeric variable. Well you can, but that just means SAS will automatically convert the number into a string using the BEST12. format. Once you have converted the data to a character string then you can no longer use the numeric MMDDYY format with the value. So SAS assumes you just left the $ off the front of your character format name. However it can't find such a character format so you get the error message.
To make sure the result has slash instead of some other delimiter you can use the MMDDYYS format.
new_date= Put( orig_date , mmddyys10.);
I would like to remove dashes from 3 to 9 digit numbers. A certain percentage of those numbers have leading zeros in them. I tried using the Compress function, but this stripped the zeros as well. What would be the best function to use?
I understand your "numbers" are actually codes with digits and dashes and you want to keep only the digits, so what you need is string processing.
The compress function in SAS has a second (optional) parameter. If you don't specify it, the function will remove all white space characters. If you do, it will remove the characters specified. So try
no_dash = compress(with_dash, '-');
Alternatively you could remove all non digit characters, using a third (also optional) parameter
no_dash = compress(with_dash, '0123456789', 'k');
The k specifies to keep instead of remove the characters specified. You can shorten this by adding the d to the third parameter, telling SAS to add all digits to the second:
no_dash = compress(with_dash, '', 'dk');
If you have stored the compressed result (with implicit conversion) in a numeric variable, that variable may need a format to get the result you want.
data _null_;
my_dashed_text = '000-90-123';
my_compressed_text = compress(my_dashed_text, '-');
attrib my_num_var
length = 8
format = z9.
;
my_num_var = compress(my_dashed_text, '-');
put (_all_) (=/);
run;
------ LOG -----
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
36:16
my_dashed_text=000-90-123
my_compressed_text=00090123
my_num_var=000090123
The Z numeric format tells SAS to add leading zeros that fill out to the specified width when displaying the number. The format is a fixed width, so a my_num_var from both "123-456" and "0-1-2-3-45-6" will display a Z9 formatted value of 000123456. SAS formatting can't make a number value look like 123456 or 0123456 when rendered through a single format specification (such a Z9)
I have a string called weight that is 85.5
I would like to convert it into a numeric 85,5 and replace the decimal seperator with a comma using SAS.
So far I am using this (messy) two step approach
weight_num= (weight*1);
format weight_num COMMAX13.2;
How can this be achieved in a less clumpsy way??
Your sample code is the recommended method of changing a variable type.
Another way is transtrn function to replace the . with a comma. This is only a good method if you don't plan to do any calculations on the values.
data have;
set sashelp.class;
keep name weight:;
weight_char=put(weight, 8.1);
run;
data want;
set have;
weight_char=transtrn(weight_char, ".", ",");
run;
proc print data=want;
run;
If you just want to change it so that commas are used for decimal point instead of periods then why not just use a simple character substitution. Do you also want to change thousands separator from comma to period? TRANSLATE() is good for that.
weight = translate(weight,',.','.,');
If you want to convert it to a number then use the INPUT() function rather than forcing SAS to convert for you.
weight_num = input(weight,comma32.);
You can then attach whatever format you want to the new numeric variable.
When we convert a numeric to character , we should use a numeric format like the following
data test ;
prodID = 001 ;
result = put(prodID , 1.) ;
run ;
proc print ;
run ;
I also tried to use a character format $1. , and it also worked
data test ;
prodID = 001 ;
result = put(prodID , $1.) ; *I am using $1. here ;
run ;
proc print ;
run ;
Question is why did the second code work ? It was not supposed to work . Should we use a numeric or character format or it does not matter ?
You do get a warning with the second code:
WARNING: Variable prodID has already been defined as numeric.
That's because you are applying a character format to a numeric variable
But the result of the put function is always character.
To convert a character variable to a numeric variable, you use the INPUT() function (which uses informats).
newvar_num = INPUT(oldvar_char, informat)
The INPUT() function is similar to reading external data using the INPUT statement. The informat tells SAS how to read the data, and it should be read as numeric. When converting from character to numeric, the informat must be the type you are converting to, so numeric.
To convert a numeric variable to a character variable, you use the PUT() function (which uses formats).
newvar_char = PUT(oldvar_num, format)
The PUT() function is similar to writing out data using the PUT statement. The format tells SAS how to output or store the data. In the PUT() function, the format must be the same type as the source variable (oldvar_num), so numeric.
The PUT() function can also be used to convert a character variable to another character variable, with a character format. See examples A and C: https://blogs.sas.com/content/sgf/2015/05/01/converting-variable-types-do-i-use-put-or-input/.
The source variable of INPUT() function must always be character, the output can be character or numeric. The output of PUT() function is always character, the input can be character or numeric.
A good explanation of informats and formats can be found here: https://libguides.library.kent.edu/SAS/Informats-Formats.