Help using Base SAS procedures

How to Read Comma-Embedded Numbers into Numeric Variable?

Reply
N/A
Posts: 0

How to Read Comma-Embedded Numbers into Numeric Variable?

Hi, I just run into another problem about data conversion -

I want to create a new numeric variable VAR_NEW to equal an existing SAS variable VAR_OLD. However, the numbers contained in VAR_OLD have embedded commas, such as "1,026", "3,085", which makes it essentially in a string format aligned in the middle of the variable column.

As a result, the value of VAR_NEW becomes missing whenever a VAR_OLD value is greater than 999. So, what is the easy way to generate VAR_NEW without the missing value issue?

Thanks a lot!

Message was edited by: SASLooker Message was edited by: SASLooker
SAS Employee
Posts: 174

Re: How to Read Comma-Embedded Numbers into Numeric Variable?

Posted in reply to deleted_user
If I understand your question correct - try

data test;
set data;
format var_new 8.;
var_new = input(var_old,best.);
run;
N/A
Posts: 0

Re: How to Read Comma-Embedded Numbers into Numeric Variable?

Posted in reply to GertNissen
Hi Geniz,

Thanks for the suggestion. I tried that, but that makes var_new entirely into missing values.

But following your direction, I came across this, and it worked perfectly -

data test;
set data;
var_new = input(var_old, comma8.);
run;

Although the result is satisfactory, I am not so sure if using COMMAw.d this way is technically correct, or I just got lucky. Because the doumentation I found about COMMAw.d typically refers to reading external file rather than to converting a SAS internal data file. So, I'd like to hear the others' confirmation or verification.
Respected Advisor
Posts: 4,173

Re: How to Read Comma-Embedded Numbers into Numeric Variable?

Posted in reply to deleted_user
The COMMAw.d and other informats tell SAS how to interprete a string for converting it into a SAS number. This string can of course be stored in a SAS character variable.
Occasional Contributor
Posts: 9

Re: How to Read Comma-Embedded Numbers into Numeric Variable?

Posted in reply to deleted_user
SASLooker,

It's not clear from your message where VAR_OLD comes from. Are you reading it in as a character variable? If so, then read it as a numeric variable using the COMMAw.d informat, as Patrick suggested.

In your INPUT statement, the code

VAR_OLD :COMMA5.

will read anything up to four digits, with or without a comma, as a numeric variable. The colon modifier tells SAS to ignore the width (5) and lets you read anything from one- to four-digit numbers. If you don't include the colon modifier and there are numbers less than 1,000, you won't get the expected results for those numbers.

If it's in a permanent SAS data set rather than one you are creating when you run your program, never mind!

liz
N/A
Posts: 0

Re: How to Read Comma-Embedded Numbers into Numeric Variable?

thanks for the tip, liz! I am dealing with a permanent SAS data, and for now, everything seems to work out. But I'll bear your suggestion in mind if I deal with other situations.
Ask a Question
Discussion stats
  • 5 replies
  • 308 views
  • 0 likes
  • 4 in conversation