DATA Step, Macro, Functions and more

Input not working to convert character to numeric values

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

Input not working to convert character to numeric values

I have visit numbers that are in character format.  I am trying to convert those values to numeric format so they will sort properly (ascending order).  The name of my visit number variable is cvisit in analysisallp.

 

I used the following code, did not get any errors, the new variable got created but did not do the conversion.  

data consort1;
set analysisallp;
keep usubjid design invarm cvisitn;
cvisitn=input(cvisit,$2.);
run;

 

The data structure is as follows for consort1:

design usubjid invarm cvisitn (in consort 1 this variable is still character)

DB          1           Rx        1

DB          1           Rx        2

 

The column attributes for cvisitn says $2. for format and informat and the radio button for character is filled in.

 

I feel like there is something obvious that I am doing wrong, but can't figure out what it is.  I am using SAS 9.4 TS level 1M3.

 

Thanks in advance for your help.


Accepted Solutions
Solution
‎04-30-2018 04:07 PM
Super User
Posts: 9,866

Re: Input not working to convert character to numeric values

A $ infornat will create character variables. Use the 2. informat instead.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎04-30-2018 04:07 PM
Super User
Posts: 9,866

Re: Input not working to convert character to numeric values

A $ infornat will create character variables. Use the 2. informat instead.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 52

Re: Input not working to convert character to numeric values

Posted in reply to KurtBremser
Thank you so much!!! I knew it was something obvious and simple. This worked! I appreciate your help. Smiley Happy
Super User
Posts: 13,283

Re: Input not working to convert character to numeric values

Posted in reply to KurtBremser

And if the variable is already character then you need to create a NEW variable to hold the numeric value. Once a variable has a type SAS really doesn't let you change it.

 

cvistnumeric = input(cvistn,f2.);

Contributor
Posts: 52

Re: Input not working to convert character to numeric values

thank you. So the f will hold the numeric value?
Super User
Posts: 13,283

Re: Input not working to convert character to numeric values


@PaulaC wrote:
thank you. So the f will hold the numeric value?

F is another way to refer to the w.d informat. It basically says, expect to read two characters into a number and does not expect any decimals, i.e integers from -9 to 99.

 

I simply don't like not referencing a more explicit format or informat name: 2.  or best2. would also work.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 108 views
  • 0 likes
  • 3 in conversation