I have the following code
data Item_Import;
SET Item_Import(RENAME=(Item_Score=cItem_Score));
Item_Score=input(cItem_Score,3.);
drop cItem_Score;
run;
Item_Import is already numeric, but it may not always be so for other datasets on which i may want to run this code.
running it when Item_Import is already numeric results in it becoming populated with '.'
how do i get it to keep its values after conversion?
Why would you need to "convert" a number to a number? Just assign the value to the new variable.
new_number = old_number ;
The first argument to the INPUT() function is a character value. If you call it with a number there SAS will convert the number into a string for you using the BEST12. format. If you then try to read just the first three characters then unless the value is very large you will be reading the leading spaces which would explain why all of the values are missing.
If you want code that will work whether the variable is numeric or character try this:
Item_Score=input(cats(cItem_Score),32.);
The CATS() function will convert either numeric or character values to string removing any leading or trailing spaces. Use the width of 32 for the informat. That is the maximum width allowed. It does not matter whether the string is shorter than the informat width or not.
This can be handled with macro %IF-%THEN-%ELSE. First you test to see if Item_score is numeric or character. If it is character, you execute the INPUT function. Otherwise you do not execute the input function. The code below is untested and is only valid in SAS 9.4M5 or later
/* UNTESTED CODE -- RUNS ON SAS 9.4M5 or LATER */
proc contents data=item_import noprint out=_contents_;
run;
proc sql noprint;
select type into :type from _contents_
where name='Item_Score';
quit;
data Item_Import2;
SET Item_Import;
%if &type=2 %then %do; cItem_Score=input(Item_Score,3.); %end;
%else %do; cItem_score=Item_Score; %end;
run;
Why would you need to "convert" a number to a number? Just assign the value to the new variable.
new_number = old_number ;
The first argument to the INPUT() function is a character value. If you call it with a number there SAS will convert the number into a string for you using the BEST12. format. If you then try to read just the first three characters then unless the value is very large you will be reading the leading spaces which would explain why all of the values are missing.
If you want code that will work whether the variable is numeric or character try this:
Item_Score=input(cats(cItem_Score),32.);
The CATS() function will convert either numeric or character values to string removing any leading or trailing spaces. Use the width of 32 for the informat. That is the maximum width allowed. It does not matter whether the string is shorter than the informat width or not.
@mcook wrote:
I have the following code
data Item_Import;
SET Item_Import(RENAME=(Item_Score=cItem_Score));
Item_Score=input(cItem_Score,3.);
drop cItem_Score;
run;
Item_Import is already numeric, but it may not always be so for other datasets on which i may want to run this code.
running it when Item_Import is already numeric results in it becoming populated with '.'
how do i get it to keep its values after conversion?
Highlighted text indicates your process is flawed. I would guess this is relying on proc import or wizard to bring in data. If the files are supposed to be the same structure, i.e. variable names and types, then you really need to learn an approach that makes each set the same when brought into SAS. The wizards and/or Proc Import will make guesses based on the content of the data. Depending on the exact approach you are using as few as 20 records are examined to set properties which can result in variable changing types from file to file with variables that are not always populated for every record or have values that sometimes contain all numeric digits and sometimes characters other than digits, differing lengths of character variables and sometimes odd treatment of date, time or datetime values.
Note that use of the construct:
Data somename;
set somename;
<other code>
run;
can be very dangerous in terms of recoding data. This completely overwrites the source the data set and if you have a logic problem the source data may not be recoverable and you have go back to reading the data.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.