Hello ,
Is it possible to extend use First.variable options in SAS. I have 5 variables . 4 of them are grouping variable and last one is count variable. I want to show count of first observation of each group. below is my code;
data test;
set test;
by var1 var2 var3;
if first.var1 and first.var2 and first.var3 and first.var4 then value6= var5;
else value6 = .;
above code just used if first.var1 condition only. I am wondering if I am doing something wrong. If so if there is other way to fix this issue,
Thank you
If you can tell us what you want then we can try to see how (of if) you can do it using FIRST. processing.
What do you mean by "count of first observation of each group"?
If you want to assign each distinct set of values of var1 to var4 a unique identifier then you would do:
data want ;
set have;
by var1 var2 var3 var4 ;
groupnum + first.var4 ;
run;
Print out your first. and last. variables to see what they are to help determine your logic.
e.g.
data test;
set test;
a1=first.var1;
a2=first.var2;
....
b1=last.var1;
b2=last.var2;
run;
Then look at the rows you want to keep and see what your logic should be.
If you do what I suggest I bet you'll figure out you need only either:
first.var1
OR
first.var4
It's rare to need the and/or combinations of them but there are cases. If you still can't figure it out, post sample data. You'll find if you post sample data it forces you to be more clear with your question, but you'll also get responses much faster...and useful ones instead of guesses.
(first.var1 and first.var2 and first.var3 and first.var4) is the same as (first.var1) by definition.
Similarly (first.var1 or first.var2 or first.var3 or first.var4) is the same as (first.var4).
Tom,
I got exactly same(first.var1 and first.var4) for (or and and ) condition. as you said.Is there a way to achieve my desire output.
thanks
If you can tell us what you want then we can try to see how (of if) you can do it using FIRST. processing.
What do you mean by "count of first observation of each group"?
If you want to assign each distinct set of values of var1 to var4 a unique identifier then you would do:
data want ;
set have;
by var1 var2 var3 var4 ;
groupnum + first.var4 ;
run;
Tom,
That was what exactly I needed. I was trying to find total number of residents for given metropolitan area (FIPS Code) with categorical demographics
thank you for your help Tom,
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!
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.