Solved
Contributor
Posts: 68

# unknown variable name and conversion to numeric

Hi,

I have a macro that calls several data sets at certain step i use a proc transpose that creates a new variable as follows CNT_&STATE with &STATE (macro variable)and  taking values like AON, KAT etc. depending on the input of the initial data set used.

Subsequently the output data set of the proc transpose is further developed but at this stage I would like to:

a) check whether var CNT_&STATE is a character or numeric var

b) if it is character var then to change it to numeric

Any suggestions more than welcome

Thank you in advance

Best regards

Nikos

Accepted Solutions
Solution
‎04-18-2012 10:30 PM
Super User
Posts: 10,787

## Re: unknown variable name and conversion to numeric

There is a function you can use to check whether it is character or numeric.

array x(4) x1-x4;

y=vtype(x(1));

put y=;

Ksharp

All Replies
Super Contributor
Posts: 1,636

## Re: unknown variable name and conversion to numeric

do you want to try something like below? you can convert it to numeric without knowing it is a character or numeric variable.

data have;

input a\$ b;

_a=a*1;_b=b*1;

cards;

1 2

2 3

;

proc contents data=have;run;

Linlin

Super User
Posts: 6,785

## Re: unknown variable name and conversion to numeric

And to end up with the same names you started with:

data have (drop=a b rename=(_a=a _b=b);

This approach can give you notes in the log about character to numeric conversion.  If those notes are troublesome to you, there are more complex ways to solve the problem.

Posts: 3,852

## Re: unknown variable name and conversion to numeric

It would be easier to check the variable being transposed and convert it to numeric before it is transposed.  Then there is no rename issue.  Post some sample data and I will show you what I'm thinking about.

Solution
‎04-18-2012 10:30 PM
Super User
Posts: 10,787

## Re: unknown variable name and conversion to numeric

There is a function you can use to check whether it is character or numeric.

array x(4) x1-x4;

y=vtype(x(1));

put y=;

Ksharp

Posts: 3,167

## Re: unknown variable name and conversion to numeric

Excellent idea, but with a little confusing example, as you should already know the variable type of your array members before you can define them.

data _null_;

c='c';

n=1;

tc=vtype(c);

tn=vtype(n);

put 'tc=' tc 'tn=' tn;

run;

Regards,

Haikuo

Super User
Posts: 10,787

## Re: unknown variable name and conversion to numeric

This example is from documentation. I Just copy it here.

Occasional Contributor
Posts: 6

## Re: unknown variable name and conversion to numeric

try something like this....

data have;

cnt_sc=1;

cnt_nc="1";

cnt_ga="1";
cnt_fl=
1;

output;

run;

proc contents data=have out=_type noprint;run;

procsql noprint;

select
name,

"_"||strip(name),

"_"||strip(name)||"="||strip(name)

into
:cntChar separated
by " ",

:NcntChar separated by" ",

:RcntChar separated by" "

from _type wheretype=2;

quit;

%put   &RcntChar;

data want(drop=&cntChar rename=(&RcntChar));

set have;

array _c  &cntChar;

array _n  &NcntChar;

do over _c;

_n=input(_c,8.);

end;

run;

proc contentsdata=want; run;

New Contributor
Posts: 3

## Re: unknown variable name and conversion to numeric

If you use var_nm = input(c_num,??8.); it will put a number when it is numeric and . when it is not.  Here is an example.

DATA test;

input c_num \$;

datalines;

123

1. 12.34
2. 12.34.5

12a4

12)1

23 1

-43

-.5

;

run;

data test1 ;

set test ;

ck_num = input(c_num,??8.) ;

run ;

This gives the following log and proc print; output.

NOTE: There were 8 observations read from the data set WORK.TEST.

NOTE: The data set WORK.TEST1 has 8 observations and 2 variables.

NOTE: DATA statement used (Total process time):

real time           0.01 seconds

cpu time            0.01 seconds

The SAS System         13:59 Monday, April 16, 2012   1

Obs    c_num       ck_num

1     123         123.00

2     12.34        12.34

3     12.34.5        .

4     12a4           .

5     12)1           .

6     23           23.00

7     -43         -43.00

8     -.5          -0.50

Occasional Contributor
Posts: 6

## Re: unknown variable name and conversion to numeric

Exactly and without error messages.

From my understanding of the task,  "a) check whether var CNT_&STATE is a character or numeric var", was refering to the variable, not its contents specifically as often times character viarbles only contain numeric data.  Also, "b) if it is character var then to change it to numeric", by default, if you change a character field to a numeric, you will lose any non-numeric data as it is nolonger compatible in the new datatype.

On the other hand, if he only wanted to change those from character to numeric where it could without data loss, then you could do another check prior to the sql statemes above to trap the character variables that you didn't want converted.

New Contributor
Posts: 3

## Re: unknown variable name and conversion to numeric

Right, I skipped the answer to the first part of the question.  Fortunately there is a very simple solution.

Use the vtype function to test if CNT_&STATE is charater or numeric.

vtype(cnt_&state); Returns 'N' if it is numeric and 'C' if it is character.

🔒 This topic is solved and locked.

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

Discussion stats
• 10 replies
• 764 views
• 0 likes
• 8 in conversation