BookmarkSubscribeRSS Feed
Jianan_luna
Obsidian | Level 7

I am currently studying course Programming 2. In lesson (summarizing data), challenge practice. I am confused about code:

if First.ParkName=1 and Last.ParkName=1 
        then output singleState;
    else output multiState;

The question and answer are following:

 
 

 

 

For my understanding, first.parkname=1 means the first occurrence of one unique parkname,  last.parkname=1 means the last occurrence of that unique parkname. If we want create a table with unique parkname, we need to use fist.parkname=1 to collect all unique name. If we combine those two statement together, thus the unique name will be duplicate, which occur twice in the output table. Here is my concerns. Please help to figure out my confusion. Thanks so much.

 

 

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26
First.ParkName=1 and Last.ParkName=1 then output

This produces a record that is outputted only when there is only one record for that parkname (in other words, when both first.parkname and last.parkname are true for the same record).

 

The explanation you stated "If we combine those two statement together, thus the unique name will be duplicate, which occur twice in the output table" is not what will happen from this code. It would happen if the code were


First.ParkName=1 OR Last.ParkName=1

and there were multiple records for that parkname, then you would get two record for that parkname.

--
Paige Miller
Jianan_luna
Obsidian | Level 7

Thanks so much sir. I still have one question, what if I use Last.parkname=1(or first.name=1) only to get the unique name for that column. Because we want to record the occurrence of unique name of parkname column, both the single statement can record the occurrence once it equal to 1. I run the code, the answer is different, can u please explain it?

PaigeMiller
Diamond | Level 26

Looking at the original code, it was creating two data sets, SINGLESTATE and MULTISTATE. Parknames that appear once get output to SINGLESTATE and parknames that appear more than once are output to MULTISTATE.

 

What you are proposing (use only last.parkname) ignores that we are trying to get two data sets, and its not clear what you mean. Please show a complete data step to make your question more clear.

--
Paige Miller
Patrick
Opal | Level 21

Below example shows you when first.<variable> and last.<variable> gets a value of 1 (TRUE) or value of 0 (FALSE).

Patrick_0-1595122650123.png

If you want to select rows from a sorted source table where there is only a single row in a by group then you need to use IF first.<variable> AND last.<variable>. That's the logic which will select the row where ParkName is BBB in above sample. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1027 views
  • 0 likes
  • 3 in conversation