BookmarkSubscribeRSS Feed
gyambqt
Obsidian | Level 7

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;

14 REPLIES 14
PGStats
Opal | Level 21

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

PG

PG
gyambqt
Obsidian | Level 7

the typo has been fixed in my original post.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

gyambqt
Obsidian | Level 7

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

gyambqt
Obsidian | Level 7

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?

Patrick
Opal | Level 21

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;

gyambqt
Obsidian | Level 7

HI patrick,

the typo has been fixed in my original post.

the value following cards statement is ab

Patrick
Opal | Level 21

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;

gyambqt
Obsidian | Level 7

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"

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;

AmitRathore
Obsidian | Level 7

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;

OzawaOZ
Calcite | Level 5

How about chenge from:

     infile "c:/file.txt";

                  To:

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

in Program B.

Ozawa

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 14 replies
  • 2869 views
  • 0 likes
  • 8 in conversation