I'm trying to output the first var3 for a first observation in a group of var1 AND var2, with output of var3 now var4.
Example data:
var1 var2 var3
1 1 100
1 1 200
2 1 300
2 1 400
2 2 500
Expected output:
var1 var2 var4
1 1 100
2 1 400
Here is my code, but it's not working as log shows first.var1 and first.var2 are uninititalized.
I'd appreciate any advice.
proc sort data=mydata out=mydata2; by var1 var2;
data mydata3;
set mydata2;
if FIRST.var1 and FIRST.var2 then
var4=var3;
run;
First. and Last. processing require a BY statement. You were almost there:
data mydata3;
set mydata2;
by var1 var2;
if FIRST.var1 and FIRST.var2 then
var4=var3;
run;
But your shown example output of
2 1 400
does not match your description of wanting the First of var1 and first of var2 which would be
2 1 300
for your given example input
First. and Last. processing require a BY statement. You were almost there:
data mydata3;
set mydata2;
by var1 var2;
if FIRST.var1 and FIRST.var2 then
var4=var3;
run;
But your shown example output of
2 1 400
does not match your description of wanting the First of var1 and first of var2 which would be
2 1 300
for your given example input
Thanks for spotting my error and providing the missing BY as solution!
You asked for output of first observations by var1 and var2.
Did you mean first.var1 and first.var2 ?
I assumed no.
In this case the output should contain 3 obs:
var1 var2 var4 1 1 100 2 1 400 2 2 500
@ballardw's solution miss OUTPUT statement. As is you will get the all input obs in output.
So the solution should be:
data mydata3;
set mydata2;
by var1 var2;
if FIRST.var2 then do;
var4=var3;
output;
end;
drop var3;
run;
Alternative solution:
data mydata3(rename=(var3=var4));
set mydata2;
by var1 var2;
if FIRST.var2 then output;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.