Hi there, I have to overwrite some character data to change to a value beginning in 0. When I go to perform the data step, SAS automatically converts the data to numeric and it is not stored properly. Here is an example of the data and code:
Existing Value for character variable ID with length 3: AAA
Desired Value for value AAA in same character variable ID with length 3: 0AA
Code:
Data work.data;
set work.dataold
length ID $3;
if ID="AAA" then ID= "0AA";
Run;
In this program SAS converts the value "0AA" to numeric, but I want it to stay as a character.
Thanks!
Please show your log.
The only reason SAS will show messages about converting to/from numeric with your posted code would be if ID already exists on DATAOLD as a numeric variable. In which case ID could never have a value like 'AA'.
Here is the results with proc contents:
Variable | Type | Len | Format |
ID | Char | 3 | $3. |
Here is the log:
DATA work.data;
813 set work.dataold;
814 length UPL_ID $3;
821 IF UPL_ID='AAA' and month >4 and year=2020 then UPL_ID="0A1" ';
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
821:64 842:63 849:63
NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
821:70 842:69 849:69
NOTE: Invalid numeric data, '0A1' , at line 821 column 64.
@DRM51 wrote:
Here is the results with proc contents:
Variable Type Len Format ID Char 3 $3.
Here is the log:
DATA work.data;
813 set work.dataold;
814 length UPL_ID $3;
821 IF UPL_ID='AAA' and month >4 and year=2020 then UPL_ID="0A1" ';NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
821:64 842:63 849:63
NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
821:70 842:69 849:69
NOTE: Invalid numeric data, '0A1' , at line 821 column 64.
And where is the detail about the variable UPL_ID???
You show a variable named ID. That is not the same as UPL_ID. You do not show any code using a variable named ID.
Copy an entire data step or procedure along with all the messages. Copy all of it from the log. Then on the forum open a text box using the </> at the top of message window and paste. This preserves the content and the diagnostic information SAS provides.
The "code" you pasted like would not run at all because after what ever edit you did there is an extra ' at the end of
then UPL_ID="0A1" ';
Which means that statement did not have an ending semicolon and would be trying to use the next line to end that statement which would very likely cause all sorts of headaches.
The NOTEs do not correspond to the log as posted. Line 821 has only 60 columns.
Please post the complete log of the step by copy/pasting it (NO editing!) into a window opened with this button:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for 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.