BookmarkSubscribeRSS Feed
pavank
Quartz | Level 8

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

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

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.

Astounding
PROC Star
You can still use end= with a null data set and a SET statement.

data want;
if 5=4 then set have;
if done then put "final values " _all_;
set have end=done;
run;

The trick is to move the statements around slightly, as shown.
The non-executing SET statement ensures that _all_ is properly defined before the PUT statement executes.
pavank
Quartz | Level 8

Hi @Astounding  

here _null_ dataset  how to use set and end option 

Astounding
PROC Star
A _null_ data set has no records. There is no such thing as a "last record". IMO, you need to clarify the question by describing the inputs and results further.
Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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.

 

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!
How to Concatenate Values

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.

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
  • 6 replies
  • 305 views
  • 2 likes
  • 5 in conversation