Dear,
I need help in my code. One of support team help me with following code. But I need one more condition to include before assigning a flag value. I need TO ASSIGN flag values only for obs with variable type='R'. Please help in the code.
I tried by sorting the data have by "type" "id". Then I used "if type='R' then do" in the code. The output also flagged obs where type='M'.
Thank you
ouput needed;
id type test visit visitwindow date flag
1 R dbp 1 1 2014-07-30 Y
1 R dbp un 1 2014-08-10
2 R dbp 1 1 2014-07-30
2 R dbp 1 1 2014-08-10 Y
3 R dbp un 1 2014-07-30
3 R dbp un 1 2014-08-10 Y
4 R dbp 1 1 2014-07-30 Y
1 M dbp 1 1 2014-07-30
1 M dbp un 1 2014-08-10
2 M dbp 1 1 2014-07-30
2 M dbp 1 1 2014-08-10
3 M dbp un 1 2014-07-30
3 M dbp un 1 2014-08-10
4 M dbp 1 1 2014-07-30
data have; input id type $ test$ visit $ visitwindow vsdtc :yymmdd10.; format vsdtc date9. ; datalines; 1 R dbp 1 1 2014-07-30 1 R dbp un 1 2014-08-10 2 R dbp 1 1 2014-07-30 2 R dbp 1 1 2014-08-10 3 R dbp un 1 2014-07-30 3 R dbp un 1 2014-08-10 4 R dbp 1 1 2014-07-30 1 M dbp 1 1 2014-07-30 1 M dbp un 1 2014-08-10 2 M dbp 1 1 2014-07-30 2 M dbp 1 1 2014-08-10 3 M dbp un 1 2014-07-30 3 M dbp un 1 2014-08-10 4 M dbp 1 1 2014-07-30 run; code: data want; do i=1 by 1 until(last.id); set have; by id type; if visit=1 then idx=i; end; do j=1 by 1 until(last.id); set have; by id type; flag=' '; if idx=j then flag='Y'; if last.id and missing(idx) then flag='Y'; output; end; end; drop i j idx; run;
Nominally you can check for a given type using conditional processing. Currently you have:
if last.id and missing(idx) then flag='Y';
You can add a comparison for TYPE:
if last.id and missing(idx) and type='R' then flag='Y';
However your code has some deeper issues.
A SORT is going to be needed before you can use the BY statement as it is currently written. Have you tried the second DATA step? Is it working for you?
Visit is treated at numeric, but is character in HAVE. There is an extra END statement. Get the code working and see if it still does not do what you need it to do.
Your code won't even run without some minor changes. However, given its current structure, it already provides what you want if provided with data that is properly sorted. e.g.,
proc sort data=have out=want; by id type; run; data want; do i=1 by 1 until(last.id); set want; by id type; if visit='1' then idx=i; end; do j=1 by 1 until(last.id); set want; by id type; flag=' '; if idx=j then flag='Y'; if last.id and missing(idx) then flag='Y'; output; end; drop i j idx; run;
Art, CEO, AnalystFinder.com
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.
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.