BookmarkSubscribeRSS Feed
anandmgjsa
Fluorite | Level 6

hi,

 

I have the following code :

 data Observer_Oct_Jan;

set Observer_Oct_Jan_New_org(rename = (Date = Chardate)) 

      Observer(rename = (Date = Chardate)); 

if not missing(chardate) then date = input(chardate,yymmdd10.));

if(date > 01-10-2020 and date < 31-01-2021);

run;

I am getting the error : variable chardate has been defined as both numeric and character.

 

Please help!!

 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

In one data set, the variable is numeric. In the other data set, the variable is character. You have to change them so they are both numeric (or both character) and then do the merge.

--
Paige Miller
anandmgjsa
Fluorite | Level 6

can you please tell me how to do that.

PaigeMiller
Diamond | Level 26

@anandmgjsa wrote:

can you please tell me how to do that.


In a data step before you do the merge, you create  a new variable of the opposite type (numeric or character), and then they will both match when you do the merge.

--
Paige Miller
ballardw
Super User

@anandmgjsa wrote:

hi,

 

I have the following code :

 data Observer_Oct_Jan;

set Observer_Oct_Jan_New_org(rename = (Date = Chardate)) 

      Observer(rename = (Date = Chardate)); 

if not missing(chardate) then date = input(chardate,yymmdd10.));

if(date > 01-10-2020 and date < 31-01-2021);

run;

I am getting the error : variable chardate has been defined as both numeric and character.

 

Please help!!

 


Additionally, when you properly address the variable type this code is wrong:

if(date > 01-10-2020 and date < 31-01-2021);

If "date" is character the values would have to be inside quotes for a comparison. Character values compared with < or > do a character by character comparison and generally do not work the way you expect.

 

If your value is a numeric actual SAS data then the comparisons involving literal date values must be in the form of either Date9. or Date7. , quoted with a followiing d:  "01JAN2020"d for example. The quotes and the D tell SAS you expect to use a date value.

 

HINT: Know your data, run proc contents on your data sets and determine which ones have date as character and numeric. Since this is the fourth essentially identical question it is time for you to learn how to examine your data and address this instead of making us guess which data set has the variable of what type.

 

Tom
Super User Tom
Super User

Note the string inside the quotes for a date literal is anything the DATE informat can read.

Tom
Super User Tom
Super User

So DATE is numeric in one of those dataset and character in the other.

Run PROC CONTENTS on the dataset to see which is which.

Or just guess it is one and change the code and if you still get errors then switch and instead modify the other one.

So if the first dataset has numeric dates you would do:

data Observer_Oct_Jan;
  set Observer_Oct_Jan_New_org(rename = ()) 
      Observer(rename = (Date = Chardate))
  ;
  format date yymmdd10.;
  date = coalesce(date,input(chardate,yymmdd10.));
  if ("01OCT2020"d < date < "31JAN2021"d) ;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 640 views
  • 1 like
  • 4 in conversation