Desktop productivity for business analysts and programmers

Array for character to numeric

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Array for character to numeric

Using SAS EG to create an array transforming 24 character diagnosis codes to numeric. This is what I have so far and it does not work. I believe it has to do with defining the _num array. Not sure how to do this?

 

DATA dxcodes2;

SET dxcodes;

ARRAY _char (24) dx2-dx25;

ARRAY _num (24) dx2-dx25;

DO i to 24;

_num{i} = input(_char{i},best8.);

END;

RUN;

 


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 1,547

Re: Array for character to numeric

[ Edited ]

DATA dxcodes2;

SET dxcodes;

ARRAY _char (24) dx2-dx25;

ARRAY _num (24) _dx2 - _dx25;

DO i=1  to 24;

_num{i} = input(_char{i}, 8.);

END;

RUN;

 

Basically you can't change the datatype of the existing char vars. You need another set of numeric array

View solution in original post


All Replies
Solution
3 weeks ago
PROC Star
Posts: 1,547

Re: Array for character to numeric

[ Edited ]

DATA dxcodes2;

SET dxcodes;

ARRAY _char (24) dx2-dx25;

ARRAY _num (24) _dx2 - _dx25;

DO i=1  to 24;

_num{i} = input(_char{i}, 8.);

END;

RUN;

 

Basically you can't change the datatype of the existing char vars. You need another set of numeric array

Super User
Posts: 23,224

Re: Array for character to numeric

1. You cannot do in place conversion, a variable cannot be changed from numeric to character or vice versa so you need to create a new variable. 

2. Diagnosis codes usually have a letter as well as codes, so be careful. 

Contributor
Posts: 23

Re: Array for character to numeric

So the reason I had originally wanted to change all the dx codes to numeric is because I tried the substring function to extract any ICD9 codes of '090' but was ending up with people whose code was 90_ (so like 902, 909 as examples). It's as if the zero was not being counted and I thought that maybe it had to do with the dx variables being character. This was the original code that was pulling incorrect codes - any insight is appreciated:

 

ARRAY DX (24) DX2-DX25;
        csyph = 0;
        DO I = 1 TO 24;
        IF SUBSTR(dx{I},1,3) = '090'   THEN csyph = 1;

END;

 

 

Super User
Posts: 23,224

Re: Array for character to numeric

Your code looks correct, but the flag is 1, if any in the row is 090.  Without your data, it’s hard to say more. 

 

 

Contributor
Posts: 23

Re: Array for character to numeric

I guess what I am trying to go for is if the first three digits of the dx code is EXACTLY = 090 instead of any sort of "090" string within the dx code existing. Does that make sense?

Super User
Posts: 23,224

Re: Array for character to numeric

Your code is correct, so I think you’re seeing some other issue.... that’s resulting in the 09X or the 90X.  

 

DX codes have the letter at the front, so E090 would not match. 

Super User
Posts: 13,283

Re: Array for character to numeric


@Krysia24 wrote:

So the reason I had originally wanted to change all the dx codes to numeric is because I tried the substring function to extract any ICD9 codes of '090' but was ending up with people whose code was 90_ (so like 902, 909 as examples). It's as if the zero was not being counted and I thought that maybe it had to do with the dx variables being character. This was the original code that was pulling incorrect codes - any insight is appreciated:

 

ARRAY DX (24) DX2-DX25;
        csyph = 0;
        DO I = 1 TO 24;
        IF SUBSTR(dx{I},1,3) = '090'   THEN csyph = 1;

END;

 

 


One of the things to check for sometimes with Zero in comparison failing is to check for the existence of capital letter O. Some fonts/displays the difference between 0 and O can be very difficult.

 

Another thing to check for might be leading blanks. The strip function removes leading and trailing blanks.

IF SUBSTR( strip(dx{I}) ,1,3) = '090'   THEN csyph = 1;

 

And yet another is to verify that the DX variables are in fact character. When you ran the posted code did you get any messages about "numeric values have been converted to character"? Which would indicate that DX are actually numeric and then substr wouldn't work as the conversion to character from numeric that SAS would use would not have a leading zero for the comparison to find.

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 133 views
  • 2 likes
  • 4 in conversation