BookmarkSubscribeRSS Feed
DRM51
Calcite | Level 5

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!

 

 

 

4 REPLIES 4
Tom
Super User Tom
Super User

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'.

 

DRM51
Calcite | Level 5

Here is the results with proc contents:

 

VariableTypeLenFormat
IDChar3$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.

ballardw
Super User

@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.

Kurt_Bremser
Super User

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:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 585 views
  • 1 like
  • 4 in conversation