BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SBKH
Calcite | Level 5
In input file there are records as below
Abc
Xyz
Tdg
Efg

I want to check if xyz is there or not

Data out_nme;
Infile inpt;
Input @3 var1 char$3.;
If var1 ='xyz' then do;
Put 'record found';
End;
Count1=index(var1,'xyz);
File out_name;
Put count1;

Here I need result for only xyz if it's present or not in count1 variable but my code is giving me result for all the records. I tried other few options as well but didn't work.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use a macro variable:

%let found = 0;

data _null_;
infile inpt;
input @3 var1 char$3.;
if var1 ="xyz" then call symputx("found","1");
run;

You can then use &found. in further code to decide what to do.

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User

Use a macro variable:

%let found = 0;

data _null_;
infile inpt;
input @3 var1 char$3.;
if var1 ="xyz" then call symputx("found","1");
run;

You can then use &found. in further code to decide what to do.

SBKH
Calcite | Level 5
Thank you for the response !
I have used the code you have provided and later trying to write found record in o/p file

Code I have added
File out_file;
Put @1 found;
Run;

This gave me 12 • row wise in my o/p file as in my input file there are 12 records.
Tom
Super User Tom
Super User

@SBKH wrote:
Thank you for the response !
I have used the code you have provided and later trying to write found record in o/p file

Code I have added
File out_file;
Put @1 found;
Run;

This gave me 12 • row wise in my o/p file as in my input file there are 12 records.

That does not make any sense in response to the answer that was marked as the solution.

 

That code did not create any variable named FOUND.

 

Please explain what you are trying to do.   Are you trying to create a DATASET?  Or are you trying to write a TEXT file?  If the later what type of TEXT file?  Just a plain listing/report file?  Are you trying to create a delimited file? Or a file with fixed length fields and fixed length records?  Or are you trying to create some other type of report file? Perhaps a PDF file or a sheet in an XLSX workbook?

 

It will help to include some small example input data and the output you expect to create from that input data.

SBKH
Calcite | Level 5
I am new here and marked solution to the first answer as I accepted the answer by mistake.

I'll try to add more details for my requirement here -

I have input Fixed length PS input file

ABC 123
DEF 567
GHI 879
XYZ 990
PQR 110
Here I need to find in my mainframe sas code if xyz is present in input file or not if it's present it should write a message in my output ps dataset as
Record found
If XYZ not ther 'Record not found '

Below is the code I have written it's working perfectly if I only write condition to check record is present but not working when I try to check not present condition (getting multiple records)-

Data inpt_fl;
Infile inpt_fl;
Input @3 jobname $char3.
@;
If jobname = 'xyz' then do ;
File out_pt;
Put 'record found';
End;
/* To check if the record presence
If index(jobname,'xyz') =0 then do;
Put 'Record not found ';
End;
Here I am getting 5 results with the below sequence
Record not found
Not found
Not found
Found
Not found.

Because input file has 5 records but I want only one result i.e. if it's present it should print
Found and if it's not present then not found.

Please help me to solve this riddle.
Thanks in advance!


Tom
Super User Tom
Super User

 

If by PS you mean a simple text file then it sounds like you want to read from one file and write to another file.

If there some reason why you also want to make the SAS dataset?  If not then just use a date _NULL_ step instead of wasting time making a dataset.

 

You don't seem to be reading the file you described in your question.  The file you showed looks like this:

ABC 123
DEF 567
GHI 879
XYZ 990
PQR 110

So it looks like you have a name in columns 1 to 3 (or possible also 4) and a number in columns 5 to 7.

Which would translate to an INPUT statement like one of these.

input jobname $3. number ;
input jobname $ 1-3 number 5-7 ;
input jobname $3. +1 number 3. ;

input jobname $4. number 3.;
input jobname $ 1-4 number 5-7 ;

You need to keep track of whether or not you have found one yet so you only write one message.  You need to know when you have hit the end of the file to know when to write the not found message.

 

So assuming you have either some JCL to define the DD names of INPT_FL and OUT_PT or you have previous SAS code with FILENAME statements define the filerefs of INPT_FL and the OUT_PT then your data step could look like this:

data joblist;
  infile inpt_fl end=eof;
  file out_pt;
  if eof and not found then put 'Record not found';
  input jobname $3. number ;
  if lowcase(jobname) = 'xyz' then do ;
     found+1;
     put 'Record found';
  end;
run;

If you don't need to make the dataset it is even easier since you can just stop when one is found.

data joblist;
  infile inpt_fl end=eof;
  file out_pt;
  if eof then put 'Record not found';
  input ;
  if lowcase(_infile_) =: 'xyz' then do ;
     put 'Record found';
     stop;
  end;
run;

If the JOBNAME field is actually longer than 4 bytes and you want to check if XYX appears anywhere in it then the IF condition can be modified to just use the INDEX() function to check if the substring exists.  Either in the JOBNAME variable or the whole input line.

if index(lowcase(jobname),'xyz') then do ;
if index(lowcase(_infile_),'xyz') then do ;

 

SBKH
Calcite | Level 5
Thank you, This worked
Really appreciate your inputs here.
SBKH
Calcite | Level 5
Message should be in a dataset, I have given these input and output dasatates names in jcls

I have one more query my code is not working specifically for record not found condition if I try to write multiple put conditions instead of only 'Record not found'

On adding multiple puts it's giving me recurring results for each record
SBKH
Calcite | Level 5
Message should be in a dataset, I have given these input and output dasatates names in jcls

I have one more query my code is not working specifically for record not found condition if I try to write multiple put conditions instead of only 'Record not found'

On adding multiple puts it's giving me recurring results for each record
Tom
Super User Tom
Super User

@SBKH wrote:
Message should be in a dataset, I have given these input and output dasatates names in jcls

I have one more query my code is not working specifically for record not found condition if I try to write multiple put conditions instead of only 'Record not found'

On adding multiple puts it's giving me recurring results for each record

Language is a tricky thing, words have multiple meanings.  Language in computer science is double tricky because normal words that have multiple meanings in other contexts are given very specific meanings (which may or may not be that close to the normal meanings) when used to discuss computers or computer languages.

 

To an old school IBM mainframe programmer a dataset is what a more modern programmer would call a simple file.  A series of lines.

 

To a SAS programmer a dataset is a special SAS specific structure that is used to store data.  It has variables and observations, not lines and columns.

 

In SAS you read from the FILE using INFILE and INPUT statements.  You write to a file using FILE and PUT statements.  But you use a dataset using a SET/MERGE/UPDATE statement.  You create a dataset using a DATA statement.

 

So again which is it that you have as input?  A SAS dataset? If so then describe the dataset.  How many variables does it have? What are their names? Their types.  For the character variables their storage length? Do any of them have formats attached to them?  What formats? How many observations does it have.  You can get this information by running PROC CONTENTS on the dataset.

 

What is it that you want to create?  A SAS dataset?  What variables should it have? What are their types? For character variables what are their storage lengths?  Do any of them need to have special display formats attached (normally only needed for date, time and datetime values)?

 

If the input is a FILE then what is its structure?  Does it have lines?  Does it used fixed length records? Or is it like a normal text file that uses variable length records that are ended by line termination characters?  How is the data on the lines organized?  Does it have values for different fields?  Are the fields fixed in length? Or do they vary from line to line?  If they vary how can you tell when one field ends and the next starts? Is a delimiter used between the fields on the line?

 

SBKH
Calcite | Level 5
Hi Tom,
I M not getting much on datasets that you are asking it's a mainframe PS output dataset where I need to write kind of a report where I have to provide jobnames and it's details like end time , completion time in tabular format,
Earlier I haven't provide these details because I thought I would be able to replace record found and not found messages to the message I want to write in output dataset.
But now when I try to add multiple put condition in record not found code is ignoring the first put line and remaining put statements are recurring which matches to the number of records.

First code you have provided I have used that one....

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1041 views
  • 1 like
  • 3 in conversation