BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Santt0sh
Lapis Lazuli | Level 10
Hi All!!

I am trying to create a variable based on the values of another variable in the same dataset.
Dataset original has 137 records and after calculations and applying various conditions the values of the Variable FLAG are 1 and 0 the sum of Variable FLAG is 29. Now I am trying to create a new variable TOT_FLAG
Where the TOT_FLAG is only has the value 1 for 29 observations and 0 for the remaining 108 observations.
I have tried using the do until for some reason my code isn't updating only 29 observations.

Please suggest.

I'm not able to post the code now but will try again in sometime


Regards,
S


1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

So we can not use the data step iteration counter, but need to make our own:

data want;
set have;
retain __n 1;
if __n lt 29 and transactions eq 1
then do;
  total_flag = 1;
  __n + 1;
end;
else total_flag = 0;
drop __n;
run;

View solution in original post

4 REPLIES 4
Santt0sh
Lapis Lazuli | Level 10
Hi Kurt,

Thank you for your quick response!!

I'm sorry I forgot to mention that I need to update all the 29 observations out of 137 observations where another Variable "TRANSACTIONS" is 1
and the remaining 108 observations needs to be 0.
I was trying the below code, the issue seems to be with _N_ here
As it will only go till _N_ = 29 but I am trying to update the 29 observations where Transactions = 1 out of 137 observations, and the remaining 108 Flag needs to be assigned 0.


DATA Transactions;
Set Transactions;
If Transactions EQ 1 and _N_ LE 29 then do;
Total_Flag = 1;
END;
ELSE Do;
TOTAL_FLAG = 0;
END;
Run;


Kindly Suggest!



Kurt_Bremser
Super User

So we can not use the data step iteration counter, but need to make our own:

data want;
set have;
retain __n 1;
if __n lt 29 and transactions eq 1
then do;
  total_flag = 1;
  __n + 1;
end;
else total_flag = 0;
drop __n;
run;
Santt0sh
Lapis Lazuli | Level 10
Thank you Kurt!!!
I worked, to update all the 29 observations I had to change LT to LE.


Regards,
S

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
  • 4 replies
  • 447 views
  • 0 likes
  • 2 in conversation