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.
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.
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?
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.
Below example shows you when first.<variable> and last.<variable> gets a value of 1 (TRUE) or value of 0 (FALSE).
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.
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.