BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ynchoir
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

4 REPLIES 4
Haikuo
Onyx | Level 15

Try this:

instead of :

char_code = put(ds_code, 7.);

use this:

  char_code = put(ds_code, $7.);

Haikuo

Linlin
Lapis Lazuli | Level 10

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;

Tom
Super User Tom
Super User

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.

ynchoir
Fluorite | Level 6

Linlin,

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

Best,

Yosh

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

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