DATA Step, Macro, Functions and more

put a tag base on variable condition

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

put a tag base on variable condition

Hi All,

 

I have a code below:-

 

 

data testing;
  infile datalines dlm='|';
  input  date $ acctno $ due $ pic $;
;
datalines;
20170801 | 12345   | 0000000 | UserA
20170802 | 12345   | 0000000 | UserA
20170803 | 12345   | 0000000 | UserX
20170804 | 12345   | 0000001 | UserA
20170805 | 12345   | 0000002 | UserC
20170806 | 12345   | 0000003 | UserC
20170807 | 12345   | 0000004 | UserA
20170802 | 22222   | 0000000 | UserX
20170803 | 22222   | 0000000 | UserB
20170804 | 22222   | 0000001 | UserA
20170805 | 22222   | 0000002 | UserA
;

proc sort data=testing;
	by acctno date;
run;

data want;
	set testing;
	by acctno date;
	retain tag;
	if first.acctno then 
		do;
			if pic NE 'UserX' then tag = 'Normal';
			else tag = 'UserX';
		end;
	else 
		tag = lag(tag);
run;

and outcome as below:-

 

 
dateacctnoduepictag
20170801123450UserANormal
20170802123450UserA 
20170803123450UserXNormal
20170804123451UserA 
20170805123452UserCNormal
20170806123453UserC 
20170807123454UserANormal
20170802222220UserXUserX
20170803222220UserB 
20170804222221UserAUserX
20170805222222UserA 

 

How can i get below output? logic is once acc's pic is UserX, then later the rest will become UserX.

 

dateacctnoduepictag
20170801123450UserANormal
20170802123450UserANormal
20170803123450UserXUserX
20170804123451UserAUserX
20170805123452UserCUserX
20170806123453UserCUserX
20170807123454UserAUserX
20170802222220UserXUserX
20170803222220UserBUserX
20170804222221UserAUserX
20170805222222UserAUserX

 

Thank in advance


Accepted Solutions
Solution
‎09-14-2017 10:44 AM
Super User
Posts: 5,513

Re: put a tag base on variable condition

If you study the documentation closely, you'll see that LAG does not retrieve the value from the previous observation.  Instead, it retrieves the value from the last time that the LAG function executed.  It's tricky.

 

The easiest fix would be to change the ELSE statement:

 

else if pic='UserX' then tag='UserX';

View solution in original post


All Replies
Solution
‎09-14-2017 10:44 AM
Super User
Posts: 5,513

Re: put a tag base on variable condition

If you study the documentation closely, you'll see that LAG does not retrieve the value from the previous observation.  Instead, it retrieves the value from the last time that the LAG function executed.  It's tricky.

 

The easiest fix would be to change the ELSE statement:

 

else if pic='UserX' then tag='UserX';

Contributor
Posts: 53

Re: put a tag base on variable condition

Posted in reply to Astounding

Astounding, thank you so much.

 

Would you mind to tell me how it work, by study the else code your give, Im confuse how SAS run the data already.

 

Best Regards

Super User
Posts: 5,513

Re: put a tag base on variable condition

One of the keys to how this works is the RETAIN statement.  Once TAG is set, it doesn't need to be re-set on each observation.  RETAIN just holds on to the value that was already there.

 

So on the first observation for each account, set the initial value for TAG.  Then just let it sit there until "UserX" is found.  For "UserX", change TAG.  Then as before ... let the current value (which is now "UserX") just sit there.

Contributor
Posts: 53

Re: put a tag base on variable condition

Posted in reply to Astounding

Thank you, Astounding.

 

Now I understand how it work. Thank for your time and guidance.

Really help me a lot

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 105 views
  • 1 like
  • 2 in conversation