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.
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 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.
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 ;
Should the message be in a dataset or a flat file?
@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?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.