Help using Base SAS procedures

SAS code problem (simple one)

Reply
Regular Contributor
Posts: 152

SAS code problem (simple one)

Hi Experts,

I have two programs, A and B, I want to know why the "if" condition is satisfied in program B but not satisfied in program A

Program A:

data a;

input;
if _infile_=:"abc" then b=1;
output;
cards;
ab

;
run;

Program B:

I create a txt file called “file” then I save the value "ab" to the file

data a;

infile "c:/file.txt";
input;

if _infile_=:"abc" then b=1;
run;

Respected Advisor
Posts: 4,659

Re: SAS code problem (simple one)

The value in the CARDS data is "ab,", not "ab"

PG

PG
Regular Contributor
Posts: 152

Re: SAS code problem (simple one)

the typo has been fixed in my original post.

Super User
Super User
Posts: 7,415

Re: SAS code problem (simple one)

What is it your trying to achieve?  Personally I would not trust any code which "guesses" things straight off.  Always be explicit and clear in your code.  Also, by what do you mean the condition is not satisfied, example inputs and outputs are descriptive.  I would first start by changing the code to:

data a;

  length tmp $20;  /* Explicitly set length so we know this is not an issue */

  input tmp $;     /* Explicitly set a variable so we know what data is being read in */

  if tmp="abc" then b=1;  /* Write the logic using the read in data - preferable using standard logic gate */

cards;

ab

;

run;

Regular Contributor
Posts: 152

Re: SAS code problem (simple one)

HI, rw9.

i Want to know why  b=1 when you run my first program but b= a missing value in my second program.

both of them are same program.

Super User
Super User
Posts: 7,415

Re: SAS code problem (simple one)

Thats why I am asking for clarification.  When I run the code posted in your original post:

data a;

input;

if _infile_=:"abc" then b=1;

output;

cards;

ab

;

run;

This yields a dataset called a in work which has one column b with missing value.  Hence I don't understand what your question is about. 

Regular Contributor
Posts: 152

Re: SAS code problem (simple one)

HI rw9,

i Am expecting both program a and b produce same result Like either b=1 or missing value in both program.

BUt when you run the two program. B=1 in first program and b=. In second program. Why?

Respected Advisor
Posts: 3,900

Re: SAS code problem (simple one)

You said it's a typo so assuming the value is "abc". When running below code in my environment then the condition is true. So I can't replicate your result.

Can you please post your real code AFTER you actually run it (copy/paste) so that we can replicate what you seem to observe?

data a;

input;

if _infile_=:"abc" then b=1;

output;

cards;

abc

;

run;

Regular Contributor
Posts: 152

Re: SAS code problem (simple one)

HI patrick,

the typo has been fixed in my original post.

the value following cards statement is ab

Respected Advisor
Posts: 3,900

Re: SAS code problem (simple one)

O.K. - now I can replicate what you're saying and it looks like a bug to me. I suggest you raise a SAS TechSupport track for this one and then let us know what answer you've got.

It's not a bug. Read 's explanation below. That explains it.

I believe the value for "b" in data set a2 should be missing.

data a1;

  infile datalines truncover;

  input;

  if _infile_=:"abc" then b=1;

  output;

  cards;

ab

;

run;

filename ab temp;

data _null_;

  file ab;

  put 'ab';

run;

data a2;

  infile ab truncover;

  input var :$10.;

  if _infile_=:"abc" then b=1;

  if var=:"abc" then c=1;

run;

Regular Contributor
Posts: 152

Re: SAS code problem (simple one)

to me, it seems when you read raw data from txt file, the record hold in the input buffer _infile_ will be applied a trim function automatically  when sas evaluate expression with colon like _infile_=: "abc".

so the right side of the expression "abc" will be truncated to "ab" .   

Trim("_infile_")="ab" or "ab" ="ab"

however if sas read data using cards statement, since input buffer _infile_ has a default length of 32367,

so the left side of the expression will be truncated to 3. 

e.g. "ab     (A lot of space until 32367 characters reached)  "="abc". Become "ab "="abc"

Super User
Posts: 5,093

Re: SAS code problem (simple one)

The answer goes back to the value of _infile_.

When reading data from a file, _infile_ is a copy of the incoming data line.  But when reading in-stream using CARDS, _infile_ is automatically 80 bytes long.  So in that case, you end up comparing three characters ("abc") to three characters ("ab ") and find that they do not match.

Super User
Super User
Posts: 6,502

Re: SAS code problem (simple one)

Because you are using the : modifier.  When you read from a text file the value of _INFILE_ matches what is in the file. If the line actually contains trailing blanks then the trialing blanks will be in the _INFILE_ variable.  When you read from CARDS then the lines are padded to a multiple of 80 bytes (ie card images).

Try it with a file that has trailing blanks on the lines.

filename sample temp;

data _null_;

  file sample;

  put 'ab';

  put 'ab ';

run;

data _null_;

  infile sample length=len;

  input;

  check= ('abc'=:_infile_);

  put check= len=  _infile_;

run;

Contributor
Posts: 21

Re: SAS code problem (simple one)

Small input from my side also. Just use 'TRIM function' with _infile_ and your if condition will match

*Program A;

data a;

  input;

  if trim(_infile_)=: "abc" then b=1;

  output;

  put _infile_ b=;

datalines;

ab

;

run;

*Program B;

data a;

  infile "C:\temp\file.txt";

  input;

  if trim(_infile_)=:"abc" then b=1;

  output;

  put _infile_ '*****' b=;

run;

N/A
Posts: 1

Re: SAS code problem (simple one)

How about chenge from:

     infile "c:/file.txt";

                  To:

     infile "c:/file.txt"   LRECL=80   PAD ;

in Program B.

Ozawa

Ask a Question
Discussion stats
  • 14 replies
  • 597 views
  • 0 likes
  • 8 in conversation