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. 


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 798 views
  • 3 likes
  • 4 in conversation