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

Hi, I am not sure if this question has been asked, and I am new to SAS, so any help would be appriciated.

 

I have a table like the following:

ID Value

1       0

1       100

1       200

1       0

2       0

2       0

2       0

2       100

2       0

3       0

3       100

3       200

 

And I would like to create a flag for the first non-zero values for each ID. The resulting table will be:

ID   Value    Flag

1       0           0

1       100       1

1       200       0

1       0           0

2       0           0

2       0           0

2       0           0

2       100       1

2       0           0

3       0           0

3       100       1

3       200       0

 

Note that the observations are already sorted by ID and then Date, so I don't want to change the order of the observations. Is there any easier way to do this? Thank you so much!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID Value;
cards;
1       0
1       100
1       200
1       0
2       0
2       0
2       0
2       100
2       0
3       0
3       100
3       200
;
run;
data want;
 set have;
 by id;
 if first.id then n=0;
 if value ne 0 then n+1;
 flag=ifn(n=1 and value ne 0,1,0);
 drop n;
run;

View solution in original post

2 REPLIES 2
Ksharp
Super User
data have;
input ID Value;
cards;
1       0
1       100
1       200
1       0
2       0
2       0
2       0
2       100
2       0
3       0
3       100
3       200
;
run;
data want;
 set have;
 by id;
 if first.id then n=0;
 if value ne 0 then n+1;
 flag=ifn(n=1 and value ne 0,1,0);
 drop n;
run;
vannestelrooy
Calcite | Level 5
Thank you! never used ifn function before!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 2510 views
  • 0 likes
  • 2 in conversation