I would like to ask if there is an example raw data :
A | 1 |
B | 2 |
3 | |
C | 4 |
5 |
And now the goal is to output each Variable ( A,B,C) contains the last record, such as:
A | 1 |
B | 3 |
C | 5 |
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.
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;
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;
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;
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;
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 .
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.
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;
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.