Programming the statistical procedures from SAS

Variable XX has been defined as both character and numeric.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Variable XX has been defined as both character and numeric.

Hi,

I have troubles in doing operation on my data 'ds_code', whose entry are all 6 or 7-digits numerics and characters.

Seeing this as a source of trouble, I converted it into character (Line 2108 of the codes below).

However, when I got to line 2117 (to identify which entry is the first for the particular ds_code), SAS shows the error message and stops.

Could anyone suggest me how to fix this?  Your feedback is greatly appreciated.

Thanks,

Yosh

2104  data ds;

2105      set monthly_prices_yield_and_returns;

2106      sp1lag = lag1(spread_over_benchmark_curve);

2107      sp3lag = lag3(spread_over_benchmark_curve);

2108      char_code = put(ds_code, 7.);

2109      drop ds_code;

2110      rename char_code = ds_code;

2111  run;

2112

2113  Data ds;

2114      retain ds_code c;

2115      old_ds_code=put(ds_code, 6.);

2116      old_c=c;

2117      set ds;

ERROR: Variable ds_code has been defined as both character and numeric.

2118      if old_ds_code=ds_code

2119          then c=old_c+1;

2120          else c=1;

2121  run;


Accepted Solutions
Solution
‎02-09-2012 02:01 PM
Super Contributor
Posts: 1,636

Variable XX has been defined as both character and numeric.

Hi,

I don't know the purpose of your code, but the modified one ran without error.

data ds;

  set ds;

  char_code = put(ds_code, 7.);

   drop ds_code;

   rename char_code = ds_code;

run;

proc contents data=ds;run;

Data ds;

length old_ds_code $ 6;

retain ds_code c;

old_ds_code=ds_code;

old_c=c;

  set ds;

     if old_ds_code=ds_code

        then c=old_c+1;

        else c=1;

run;

proc print;run;

View solution in original post


All Replies
Respected Advisor
Posts: 3,147

Variable XX has been defined as both character and numeric.

Try this:

instead of :

char_code = put(ds_code, 7.);

use this:

  char_code = put(ds_code, $7.);

Haikuo

Solution
‎02-09-2012 02:01 PM
Super Contributor
Posts: 1,636

Variable XX has been defined as both character and numeric.

Hi,

I don't know the purpose of your code, but the modified one ran without error.

data ds;

  set ds;

  char_code = put(ds_code, 7.);

   drop ds_code;

   rename char_code = ds_code;

run;

proc contents data=ds;run;

Data ds;

length old_ds_code $ 6;

retain ds_code c;

old_ds_code=ds_code;

old_c=c;

  set ds;

     if old_ds_code=ds_code

        then c=old_c+1;

        else c=1;

run;

proc print;run;

Super User
Super User
Posts: 6,706

Variable XX has been defined as both character and numeric.

The first place that SAS sees reference to DS_CODE where it can infer a data type is inside the PUT function call.   Since it is using a numeric format it defines DS_CODE as numeric. Then when it pulls in the variable from the dataset DS is discovers that there is type mismatch.

Occasional Contributor
Posts: 7

Variable XX has been defined as both character and numeric.

Linlin,

Thank you so much!  That solved!!  I fully appreciate the answers from the others.

Best,

Yosh

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 15547 views
  • 3 likes
  • 4 in conversation