Hi. I'm trying to create a new variable for later summing to get a total count for unique conditions.
My data looks like this:
My code is this:
by id from_dt to_dt ;
if first.id and first.from_dt and first.to_dt then visits = 1;
It produces this:
I understand why I'm getting the output I am, but I'm trying to get this result:
I need a visits = 1 for each ID that has a different from/to dates, but I only need a visits = 1 when the ID has the same from/to dates.
See if this does what you want:
data services_test; set services; by id from_dt to_dt ; if first.from_dt and first.to_dt then visits = 1; run;
You really need to consider providing examples with a bit more complexity. All of your From and To dates are identical. So I can't really be sure that the First.to_dt is even needed.
When you have multiple By variables you get combinations of the First and Last variables. so you were, by requiring First.ID excluding any result for other than the first observation of each Id.
In some complex needs for mixes of First and Last variable I find it helpful sometimes to ADD the values so I can see if my logic is correct.
data services_test; set services; by id from_dt to_dt ; F_id = first.id; F_From = first.from_dt; F_to = first.to_dt; run;
and look at those (and sometimes the Last versions as well).
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.