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

I have a dataset below:

date     id    first

MAY     1     1

MAY     1      

JUN      1         

SEP     2     1

SEP     2       

 

I want record with same date to mark as first=1, like

 

date     id    first

MAY     1     1

MAY     1      1

JUN      1         

SEP     2     1

SEP     2     1

 

group by id date. 

 

cannot use retain, cause it failed as 

date     id    first

MAY     1     1

MAY     1      1

JUN      1    1        -----------I don't want JUN to be as first=1, I just want all same date MAY to be first=1. 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

What about September?

 

You're correct that RETAIN may not work - and it likely own't with a variable already present. But if you create a new variable and work with that it will work fine. 

 

data have;
    input date :$3. id first;
    cards;
MAY     1     1
MAY     1     . 
JUN      1    .     
SEP     2     1
SEP     2     .  
;

data temp;
    set have;
    by date notsorted;
    retain flag;

    if first.date and first=1 then
        flag=1;
    else if first.date then
        call missing(flag);
run;

data want;
    set temp;
    where flag=1;
run;

@jojozheng wrote:

I have a dataset below:

date     id    first

MAY     1     1

MAY     1      

JUN      1         

SEP     2     1

SEP     2       

 

I want record with same date to mark as first=1, like

 

date     id    first

MAY     1     1

MAY     1      1

JUN      1         

SEP     2     1

SEP     2     1

 

group by id date. 

 

cannot use retain, cause it failed as 

date     id    first

MAY     1     1

MAY     1      1

JUN      1    1        -----------I don't want JUN to be as first=1, I just want all same date MAY to be first=1. 


 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Why did you start a new thread?

What's wrong with the one you created here?

Did you try all the solutions given (including @Astounding's?

If you did, explain why they don't suit.

And of course, clearly provide the expected output.

Jagadishkatam
Amethyst | Level 16
data have;
input date :$3.    id    first;
cards;
MAY     1     1
MAY     1     . 
JUN      1    .     
SEP     2     1
SEP     2     .  
;

proc sort data=have;
by date;
run;


data want;
do until(last.date);
set have;
by date;
retain cnt;
if first.date then cnt=1;
else cnt+1;
end;
do until(last.date);
set have;
by date;
if cnt>1 then flag=1;
else flag=.;
output;
end;
run;
Thanks,
Jag
Reeza
Super User

What about September?

 

You're correct that RETAIN may not work - and it likely own't with a variable already present. But if you create a new variable and work with that it will work fine. 

 

data have;
    input date :$3. id first;
    cards;
MAY     1     1
MAY     1     . 
JUN      1    .     
SEP     2     1
SEP     2     .  
;

data temp;
    set have;
    by date notsorted;
    retain flag;

    if first.date and first=1 then
        flag=1;
    else if first.date then
        call missing(flag);
run;

data want;
    set temp;
    where flag=1;
run;

@jojozheng wrote:

I have a dataset below:

date     id    first

MAY     1     1

MAY     1      

JUN      1         

SEP     2     1

SEP     2       

 

I want record with same date to mark as first=1, like

 

date     id    first

MAY     1     1

MAY     1      1

JUN      1         

SEP     2     1

SEP     2     1

 

group by id date. 

 

cannot use retain, cause it failed as 

date     id    first

MAY     1     1

MAY     1      1

JUN      1    1        -----------I don't want JUN to be as first=1, I just want all same date MAY to be first=1. 


 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3 replies
  • 1388 views
  • 3 likes
  • 4 in conversation