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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
Reeza
Super User

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.

Reeza
Super User

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.

Tom
Super User Tom
Super User

(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).




learner_sas
Quartz | Level 8

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

Tom
Super User Tom
Super User

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;

learner_sas
Quartz | Level 8

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,

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2762 views
  • 0 likes
  • 3 in conversation