BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pochemuchka
Fluorite | Level 6

Hi, I'm sorry to repeat same topic, but i couldn't find in existing "IF-THEN" posted discussions to match my trouble.
I have can't figure out very simple scenario with "IF-THEN" statement and log issue "Numeric values have been converted to character values":

Here is my code:

data have;
input type xxtype $ oldvar;
cards;
1 num 0
1 num 0
1 num 22
1 num 5
1 num 0
1 num 10
;
run;

data want;
set have;

length check newvar $200;
if xxtype="num" then do; check="check1: "||strip(put(type,best12.)); newvar=strip(put(oldvar,best12.)); end;
else if xxtype="char" then do;
put "type is character=" type;
check="check2: "||strip(put(type,best12.));
newvar=strip(oldvar);
end;
run;

Here is log issue:
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).

ELSE statement should never execute, nevertheless it seem SAS is tripping over the newvar=strip(oldvar); line.

If I missed very obvious logical fault, I apologize for my silly question.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You cannot treat a variable as both numeric and character in the same data step.  You might consider using macro code to only generate the right type of statements, but that is not needed if you use either CATS() or VVALUE().

%let myvar=oldvar;
data want;
  set have;
  length newvar $200 ;
   newvar=vvalue(&myvar);
run;

VVALUE() will give you the formatted value and CATS() will give you the raw value. 

View solution in original post

6 REPLIES 6
smantha
Lapis Lazuli | Level 10

That is a one time note of SAS saying that there is an application of a character function to a numeric variable as SAS realises that oldvar is a numeric variable based on input.

Pochemuchka
Fluorite | Level 6
Thank you Smantha. Is there a better way to populate newvar based on type of oldvar without having SAS log issue?
Tom
Super User Tom
Super User

One issue is that you created XXTYPE in the first data step and are referencing TYPE in the second data step.

Another issue is that you are calling the character function STRIP() put giving it a numeric variable as the input.

Are you trying to convert the numeric variable to character?  What format do you want to use to convert the number into text?

You can use CATS() to automatically convert the numeric value into a string. It will use BEST32 format instead of the BEST12 format.  

You can also use VVALUE() function to get the formatted value of the variable.

Pochemuchka
Fluorite | Level 6
Hi Tom, thank you for looking into this. I'm trying to macromize "want" datastep: pass oldvar either char or numeric and produce newvar only as character. For example, OldVar=123 => NewVar='123' or OldVar='free text' => NewVar='free text'. Thus I created condition if xxtype='char' then just strip OldVar, if xxtype='num' then convert OldVar to character. However, SAS precompiles and outputs false log warning.
Tom
Super User Tom
Super User

You cannot treat a variable as both numeric and character in the same data step.  You might consider using macro code to only generate the right type of statements, but that is not needed if you use either CATS() or VVALUE().

%let myvar=oldvar;
data want;
  set have;
  length newvar $200 ;
   newvar=vvalue(&myvar);
run;

VVALUE() will give you the formatted value and CATS() will give you the raw value. 

Pochemuchka
Fluorite | Level 6
Tom, CATS() works beautifully. I will study VVALUE() -- always fun to learn new functions. Thank you so much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 873 views
  • 0 likes
  • 3 in conversation