BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AJ_Brien
Quartz | Level 8

Hello,

 

sample dataset provided below:

 

I'm trying to get the last observation of 'id' which is '893068', such that I can use that value later.

 

acc_noid
6530792813068
8524336823068
60634079853068
66065307863068
32530792873068
78530792893068

 

I tried 2 approaches so far:

 

Approach 1: This is the code that I'm using:

proc sort data =abc; 
by id; 
run;

proc sql;
select id
 into :left
 from abc
 where last.id=1
;
quit;

I'm getting the following error:

ERROR: Unresolved reference to table/correlation name last.

 

Approach 2: Then I tried the following code:

Data _null_;
Set abc;
by id;
Retain id;
If last.id then 
left = id;
put left=;
run;

which runs perfectly, but when I try to use the value of left in another query, it gives me an error:

proc sql;
create table checks as 
select * from abc 
where id= left;
quit;

ERROR: The following columns were not found in the contributing tables: left.

 

The errors make sense to me but I'm not able to sort these. Would be great if there is a better way to do this.

 

Appreciate the guidance!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

From your first piece of code, I assume you want to put the value in a macro variable. In that case do

 

data have;
input acc_no $	id $;
datalines;
6530792	813068
8524336	823068
60634079	853068
66065307	863068
32530792	873068
78530792	893068
;

data want;
   set have end=lr;
   if lr then call symputx('lastid', id);
run;

%put &lastid.;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

From your first piece of code, I assume you want to put the value in a macro variable. In that case do

 

data have;
input acc_no $	id $;
datalines;
6530792	813068
8524336	823068
60634079	853068
66065307	863068
32530792	873068
78530792	893068
;

data want;
   set have end=lr;
   if lr then call symputx('lastid', id);
run;

%put &lastid.;
PaigeMiller
Diamond | Level 26

@AJ_Brien wrote:

 

Approach 2: Then I tried the following code:

Data _null_;
Set abc;
by id;
Retain id;
If last.id then 
left = id;
put left=;
run;

which runs perfectly, but when I try to use the value of left in another query, it gives me an error:

proc sql;
create table checks as 
select * from abc 
where id= left;
quit;

ERROR: The following columns were not found in the contributing tables: left.

 

The errors make sense to me but I'm not able to sort these. Would be great if there is a better way to do this.

 

Appreciate the guidance!


You create a _null_ data set where variable left obtains a value. You can't use a _null_ data set in the next SQL, and left does not exist in ABC which what SQL is looking at. But anyway, @PeterClemmensen has a compact method that works.

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 2934 views
  • 0 likes
  • 3 in conversation