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

I would like to ask if there is an example raw data :

A1
B2
 3
C4
 5

And now the goal is to output each Variable ( A,B,C) contains the last record, such as:

A1
B3
C5

How can I compute the data steps to make the missing variable ( e.g. the empty space below "B" ) also followed by the previous "B" and treat them as the same observation? And also how to compare between Variables to make them output different variable names? 

 

I have do some research that we can create the two variables for identify (A,B,C) by something call Var1 and Var2, then it can read the raw data record for Var1 and other variables from the same record. However, I feel confused with this method. 

 

Is there any ways to achieve the above mentioned goal? Thank you for your help. 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Why do you have to accomplish it via one data step ?

 

data have;
infile cards expandtabs;
input  x $ y;
cards;
A	1
B	2
. 	3
C	4
. 	5
;
run;
data want;
 length x $ 40;
 retain x;
 merge have(rename=(x=xx)) have(keep=x rename=(x=_x) firstobs=2) end=last;
 if not missing(xx) then x=xx;
 if not missing(_x) or last;
 drop xx _x;
run;

View solution in original post

8 REPLIES 8
Ksharp
Super User
data have;
infile cards expandtabs;
input  x $ y;
cards;
A	1
B	2
. 	3
C	4
. 	5
;
run;
data temp;
 set have;
 length xx $ 40;
 retain xx;
 if not missing(x) then xx=x;
drop x;
run;
data want;
 set temp;
 by xx notsorted;
 if last.xx;
run;
Ksharp
Super User
data have;
infile cards expandtabs;
input  x $ y;
cards;
A	1
B	2
. 	3
C	4
. 	5
;
run;
data temp;
 set have;
 length xx $ 40;
 retain xx;
 if not missing(x) then xx=x;
drop x;
run;
data want;
 set temp;
 by xx notsorted;
 if last.xx;
run;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

 

data have;
infile cards expandtabs;
input var1 $ var2;
cards;
A	1
B	2
. 	3
C	4
.	5
;

data have2;
	 set have;
	 retain holder;
	 if not missing(var1) then holder=var1;
run;
data want(drop=holder);
	 set have2;
	 by holder notsorted;
	 if last.holder;
	 var1=holder;
run;

 

 

Kitty28
Calcite | Level 5

Sorry, I may need to say that the goal have to be achieved within ONE data set step..... This is the restriction for this question.

Thank you for your help .

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

You should have noted those requirements.  If you were the manager of a shop you would be calling someone back to work or having them redo their work because of poorly defined request.

 

Robot Mad

Ksharp
Super User

Why do you have to accomplish it via one data step ?

 

data have;
infile cards expandtabs;
input  x $ y;
cards;
A	1
B	2
. 	3
C	4
. 	5
;
run;
data want;
 length x $ 40;
 retain x;
 merge have(rename=(x=xx)) have(keep=x rename=(x=_x) firstobs=2) end=last;
 if not missing(xx) then x=xx;
 if not missing(_x) or last;
 drop xx _x;
run;
Kitty28
Calcite | Level 5
Thanks for your help. As I am doing some testing with my friends, so as to make some changes to it.
Anyway the question is well-solved !
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

If your question has been solved then you and your classmates should mark the those answers as approved solutions for the answer that solved your original question.  That would give credit to the individual that solved your question and it closes unanswered question in the SAS communities Q.

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2512 views
  • 1 like
  • 3 in conversation