data _null_;
input sno name $25.;
call symput('sno',sno);
call symput('mvar',name);
%put &sno.= &mvar.;
cards;
1 Alabama
2 Alaska
3 Arizona
4 Hawaii
5 Iowa
6 Kansas
1 Alabama
4 Hawaii
;
run;
Hi Experts,
Here if we have _null_ dataset so how to find last record typically we use end=last option in set statment
here i use call symput method is it correct or any alternative method please tell me
This has been answered before here.
filename FT15F001 temp;
data _null_;
infile FT15F001 end=LASTOBS;
input SNO NAME :$25.;
if LASTOBS ;
call symputx('mvar', NAME);
call symputx('sno ', SNO );
S = symget('sno ');
N = symget('mvar');
putlog SNO= NAME= S= N=;
parmcards;
1 Alabama
2 Alaska
3 Arizona
4 Hawaii
5 Iowa
6 Kansas
1 Alabama
4 Hawaii
;;;;
run;
SNO=4 NAME=Hawaii S=4 N=Hawaii
Pay attention to when the macro variables are resolved. &macvar
is resolved before the data step is executed.
Hi @Astounding
here _null_ dataset how to use set and end option
Since you have your %PUT statement before the end of the DATA step code boundary (which is established by the CARDS/DATALINES block), SAS will resolve it before the DATA step is compiled and runs, so the macro variables won't exist yet. If you resubmit the step, you'll display the values coming from the previous execution.
Your description of the problem is confusing. Your code does not have any SET statement.
You can use the END= dataset option on a dataset that has no observations (even a dataset that has no variables if that is what you meant by a NULL dataset.)
Most data steps do not end at the last statement. Instead they end when the statement reading in the data runs out of data. So in your example that is the INPUT statement. Any data step statements after the INPUT will never execute for an empty dataset.
So do the work before the SET statement. For example this data step will set the macro variable NOBS to 0 when there are no observations read by the SET statement.
data _null_;
if eof then call symputx('nobs',_n_-1);
set have end=eof;
run;
When reading from a text file you can use the END= option of the INFILE statement. But that does not work for in-line data. If you want to detect when INPUT as read the last record from in-line data then you will need to use the EOF= option instead. For that option you specify the statement label that control should jump to when the end of file has been reached.
data _null_;
infile cards eof=last;
input sno name $25.;
call symputx('sno',sno);
call symputx('mvar',name);
return;
last:
call symputx('nobs',_n_-1);
cards;
1 Alabama
2 Alaska
3 Arizona
4 Hawaii
5 Iowa
6 Kansas
1 Alabama
4 Hawaii
;
%put &=nobs &=sno &=mvar;
A couple of notes.
1) For this problem it does not matter if you know when the end was reached since you can just keep overwriting the macro variables as you go along. I added the other macro variable just so you could see that something actually could happen after the end of the data.
2) You almost never want to use the ancient CALL SYMPUT() routine. Use the more powerful and easier to use CALL SYMPUTX() instead. The only reason to use CALL SYMPUT() is if you want the macro variables to contain leading and/or trailing spaces.
Hello @pavank
Looks like you want to clarify if your method of finding the last record is correct or not.
Yes it is.
Often SAS provides multiple approaches to do the same thing.
Anything that one is comfortable with and gives the correct result is OK.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.