0
votes

I am new in SAS and am currently working at a rather large data set. I'm getting a error of the type:

NOTE: Invalid numeric data, 'dev' , at line 1304 column 173.

I want to create an %if conditional statement of this type:

if input data is numerical then continue else if format.20 .

How would I go about doing this, thank you very much in advance and please note this is my first 3 hours in using SAS. I have experience in python.

code currently looks like this:

      dateVar=year(&dateVar.)*100+month(&dateVar.);
        
        

        if &devSampleStart.<=dateVar<=&devSampleEnd. then
            sample='dev'; 
            
        else if &baseSampleStart.<=dateVar<=&baseSampleEnd. then 
                   sample='base';
        else if &recSample.=dateVar then 
                   sample='rec ';
        else       sample='    ';

Copy of the log:

MPRINT(CALCMIGRATIONMATRIX):   dateVar=year(snapshot_date1)*100+month(snapshot_date1);
SYMBOLGEN:  Macro variable DEVSAMPLESTART resolves to 200709
SYMBOLGEN:  Macro variable DEVSAMPLEEND resolves to 201809
MPRINT(CALCMIGRATIONMATRIX):   if 200709<=dateVar<=201809 then sample='dev';
SYMBOLGEN:  Macro variable BASESAMPLESTART resolves to 201909
SYMBOLGEN:  Macro variable BASESAMPLEEND resolves to 201909
MPRINT(CALCMIGRATIONMATRIX):   else if 201909<=dateVar<=201909 then sample='base';
SYMBOLGEN:  Macro variable RECSAMPLE resolves to 202009
MPRINT(CALCMIGRATIONMATRIX):   else if 202009=dateVar then sample='rec ';
MPRINT(CALCMIGRATIONMATRIX):   else sample='    ';
SYMBOLGEN:  Macro variable DATEVAR resolves to snapshot_date1
SYMBOLGEN:  Macro variable BASESAMPLEEND resolves to 201909
MPRINT(CALCMIGRATIONMATRIX):   if mod(month(snapshot_date1),100)=mod(201909,100) then yearlyTimeSlice=1;
MPRINT(CALCMIGRATIONMATRIX):   else yearlyTimeSlice=0;
SYMBOLGEN:  Macro variable ACCOUNTID resolves to account_id
SYMBOLGEN:  Macro variable RATINGVAR resolves to rating
MPRINT(CALCMIGRATIONMATRIX):   keep account_id rating dateVar sample yearlyTimeSlice;
MPRINT(CALCMIGRATIONMATRIX):   run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      1304:173   1304:34    1304:120   1304:159   
NOTE: Invalid numeric data, 'dev' , at line 1304 column 173.
1
So the error is being caused by the variable SAMPLE being defined as numeric instead of character?Tom
the ' ' format should be more inclusive than simply = , so I think the raw data may be formatted wrong but I can't just delete that line. I expect it in a character format, yes.py14ak
Run a PROC CONTENTS on your SAS dataset and check the TYPE and LENGTH (note that FORMAT in SAS is just how to display the values, not how the values are stored.). Are your date variables numeric or character? Is your SAMPLE variable numeric or character (or non-existent?).Tom
To use the macro processor to generate different code based on the how the variable is defined you need to test the variable definition before starting to define the data step that will use it. github.com/sasutils/macros/blob/master/varexist.sasTom
@Tom Proc says they are numeric of length 8,format ddmmyy10py14ak

1 Answers

0
votes

If your date variables are really numeric with a date format attached (like DDMMYY10.) then the error is not coming from the testing of the date values.

So it sounds like SAMPLE is either an existing numeric variable. Or some earlier code in your data step is referring to SAMPLE and it is causing SAS to define the variable as numeric.

To change the code you generate based on the type of an existing variable you need to test the variable before starting to generate the code that uses it. For example you could set a macro variable that your macro could test and decide what code to generate.

If you want to create SAMPLE as a new variable then perhaps you just want to conditionally drop any existing variable with that name.

data want;
  set have (
%if %varexist(have,sample) %then %do;
    drop=sample 
%end;
  );
  length sample $4 ;
  if &devSampleStart.<=dateVar<=&devSampleEnd. then sample='dev'; 
  else ...