BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JH74
Obsidian | Level 7

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:

IDfrom_dtto_dt
12306JAN202106JAN2021
12313JAN202113JAN2021
12320JAN202120JAN2021
12327JAN202127JAN2021
45603FEB202103FEB2021
45604FEB202104FEB2021
78906MAR202106MAR2021
78906MAR202106MAR2021
78906MAR202106MAR2021

 

My code is this:

data services_test;
set services;
by id from_dt to_dt ;
if first.id and first.from_dt and first.to_dt then visits = 1;
run;

 

It produces this:

IDfrom_dtto_dtVisits
12306JAN202106JAN20211
12313JAN202113JAN2021.
12320JAN202120JAN2021.
12327JAN202127JAN2021.
45603FEB202103FEB20211
45604FEB202104FEB2021.
78906MAR202106MAR20211
78906MAR202106MAR2021.
78906MAR202106MAR2021.

 

I understand why I'm getting the output I am, but I'm trying to get this result:

IDfrom_dtto_dtVisits
12306JAN202106JAN20211
12313JAN202113JAN20211
12320JAN202120JAN20211
12327JAN202127JAN20211
45603FEB202103FEB20211
45604FEB202104FEB20211
78906MAR202106MAR20211
78906MAR202106MAR2021.
78906MAR202106MAR2021

.

 

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.  

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data services_test;
set services;
by id from_dt to_dt ;
if first.id or first.from_dt then visits=1;
run;

Not 100% sure this will scale to your full requirements.

View solution in original post

2 REPLIES 2
Reeza
Super User
data services_test;
set services;
by id from_dt to_dt ;
if first.id or first.from_dt then visits=1;
run;

Not 100% sure this will scale to your full requirements.

ballardw
Super User

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 546 views
  • 1 like
  • 3 in conversation