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

Hi I am trying to keep the first occurence of a variable and set the subsequent values of the variables to missing

My data looks like this

 

Data have;

input  id time AMI;

datalines;

 1 1  0

 1 2  0

 1 3  1

 1 4  0

 2  1  1

 2  2  0

 2  2  1

 2  4  0

run;

 

I would like my data to look like this

ID time AMI

 1 1  0

 1 2  0

 1 3  1

 1 4  .

 2  1  1

 2  2  .

 2  2  .

 2  4  .

 

Basically, I only want to assess the first occurence of AMI

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Assuming data is sorted by ID you can do:

 

data want;

 set have;

  by ID;

       retain flag; drop flag;

 

      if first.id then flag=0;

      if flag=1 then ami=.;

      if ami=1 and flag=0 then flag=1;

run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Assuming data is sorted by ID you can do:

 

data want;

 set have;

  by ID;

       retain flag; drop flag;

 

      if first.id then flag=0;

      if flag=1 then ami=.;

      if ami=1 and flag=0 then flag=1;

run;

SimRock
Obsidian | Level 7

Thank you very Shmuel. It works perfectly

Best

PGStats
Opal | Level 21

I would do:

 

data want;
do until(last.id);
    set have; by id;
    if AMImet 
        then call missing(newAMI);
        else newAMI = AMI;
    output;
    if AMI then AMImet = 1;
    end;
drop AMI AMImet; rename newAMI=AMI;
run;
PG
SimRock
Obsidian | Level 7

Thank you very PGStats. Your codes works awesome too.

Thanks so much

Best

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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