Sas datatype Conversion Question

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

Sas datatype Conversion Question

I have a process that loops through datasets that have the same 2 variables (1 is a key, 1 is a column called "BADCOLUMN"). Basically the BADCOLUMN can sometimes be character and sometimes it can be numeric. My process is taking all of these datasets and appending them together, so I obviously need this column to always be character.

However, I tried the following code below, and I get the Error "Format $best was not found or could not be loaded",  whenever I try to convert the column into a character when it is already a character. Sas is basically saying, your asking for a numeric informat, but this column is character, therefore I am going to error out. However my code should NEVER allow the dataset to see that statement if the variable is already character (or so I thought).

data mydata_boo;

  infile datalines delimiter=',';

  input key BADCOLUMN $;

  datalines; 

1,Sales

2,Acctng

;

data mydata_YAY;

set mybata_boo;

if vtype(BADCOLUMN)="N" then newvar=strip(put(BADCOLUMN,best32.));

else newvar=BADCOLUMN;

run;

Theoritically I thought that since the vtype of badcolumn is always C (in the mydata_boo), then the if statement will never trigger, and the variable newvar would be defined to equal the BADCOLUMN.  On the first row read-in, sas would read in the 2 base variables variables into the Input Data Vector, and then get to the if statement, find it to be false, move to the else statement, define newvar as character and continue about its business.

Can anyone explain to me why this is not the case? I am confused as to why the system is trying to run the newvar=strip(put(BADCOLUMN,best32.)) statement when the if condition is never true.

Thanks so much!


Accepted Solutions
Solution
‎03-29-2013 08:25 PM
Respected Advisor
Posts: 4,173

Re: Sas datatype Conversion Question

Posted in reply to Anotherdream

The problem you're facing is that syntax checking happens during the data step compilation phase and using "best32." on a character variable then is a syntax error.

SAS(R) 9.3 Language Reference: Concepts, Second Edition

I wouldn't have thought about using "putn/putc" so thanks for this one Tom.

Another a bit "brute force" method is using "cats()" as this function deals with both character and numeric.

data mydata_YAY;

  set mydata_boo;

  length Newvar $20;

  Newvar=cats(BADCOLUMN);

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,039

Re: Sas datatype Conversion Question

Posted in reply to Anotherdream

Use the PUTN or PUTC function as these will allow the format to be variable rather than a compile time constant.

Solution
‎03-29-2013 08:25 PM
Respected Advisor
Posts: 4,173

Re: Sas datatype Conversion Question

Posted in reply to Anotherdream

The problem you're facing is that syntax checking happens during the data step compilation phase and using "best32." on a character variable then is a syntax error.

SAS(R) 9.3 Language Reference: Concepts, Second Edition

I wouldn't have thought about using "putn/putc" so thanks for this one Tom.

Another a bit "brute force" method is using "cats()" as this function deals with both character and numeric.

data mydata_YAY;

  set mydata_boo;

  length Newvar $20;

  Newvar=cats(BADCOLUMN);

run;

Super Contributor
Posts: 418

Re: Sas datatype Conversion Question

Hehe using the cats options is exactly what I did to get around that problem, so that's good to know! And oh okay that makes sense, I didn't even think that the syntax checking happened before variable read in.

Thanks for this!

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 318 views
  • 0 likes
  • 3 in conversation