DATA Step, Macro, Functions and more

How to use character value similar to numeric value

Reply
Super Contributor
Posts: 307

How to use character value similar to numeric value

Dear

Some one from support helped me in the before. It worked for me until there is an obs I need to modify my code. Please help.

I highlighted in blue where i am getting wrong in results and code. How to exclude visit greater than '6' which is in character as cy9.

 

Thank you 

 

 

output  needed;

id      param        aval       visit            date

 1           bor         pr        cy6            2016-11-10

2           bor         sd           cy4           2016-08-02

3           bor         pr           cy5           2016-11-10

4           bor         cr           cy4           2016-08-02

5           bor         cr           cy4           2016-08-02

 

output getting

id      param        aval       visit            date

 1           bor         pr        cy6            2016-11-10

2           bor         sd           cy4           2016-08-02

3           bor         cr           cy9           2016-12-10

4           bor         cr           cy4           2016-08-02

5           bor         cr           cy4           2016-08-02


data ONE ;
  input ID TEST $ ORRES $ VISIT $ DATE $10.;
datalines;
1 ovrp sd cy5 2016-08-02 
1 ovrp pr cy6 2016-11-10
1 ovrp pd cy7 2016-12-10
2 ovrp sd cy4 2016-08-02 
2 ovrp pd cy5 2016-11-10
2 ovrp pd cy6 2016-11-10
3 ovrp sd cy4 2016-08-02 
3 ovrp pr cy5 2016-11-10
3 ovrp cr cy9 2016-12-10
4 ovrp cr cy4 2016-08-02 
4 ovrp pr cy5 2016-11-10
5 ovrp cr cy4 2016-08-02 
5 ovrp pr cy5 2016-11-10
5 ovrp sd dvs 2016-11-10
;
proc sort data=ONE out=SORTED(index=(ID_ORRES=(ID ORRES)));
  by ID DATE  VISIT;
run;
data TWO; 
  set SORTED;
  by ID;

  %* init;
  if first.ID then ID_DONE=0;
  if ID_DONE=0;
  ORDER=6-whichc(ORRES,'cr','pr','sd','pd','ne');
  output;

  if VISIT >='cy6' then do; "in my  code I used visit='cy6"
    ID_DONE+1;
    return;
  end;

  NEXTOBS=_N_+1;
  set SORTED(keep=ID ORRES rename=(ID=NEXT_ID ORRES=NEXT_ORRES) ) point=NEXTOBS;
  if ID=NEXT_ID and NEXT_ORRES='pd' then do;
    ID_DONE+1;
    return;
  end;
  keep ID TEST ORRES VISIT DATE ORDER;
run;
proc sql;
  create table THREE as 
  select ID, TEST, ORRES, VISIT, DATE
  from TWO
  group by ID
  having ORDER=max(ORDER);
quit;

 

Respected Advisor
Posts: 2,647

Re: How to use character value similar to numeric value

Posted in reply to knveraraju91

6 is 'cy9'? 

 

Is that all you want to do is exclude where visit = 'cy9'?? Or is this a more general problem, in which case you haven't explained the problem at all.

 

You can exclude from any analysis 'cy9' with

 

set sorted(where=(visit^='cy9'));
--
Paige Miller
Super Contributor
Posts: 307

Re: How to use character value similar to numeric value

Posted in reply to PaigeMiller

Thank you for quick reply. 

 

I will try to explain as much as I can.

 

In my code i highlighted in blue  "if VISIT='cy6' or "index(visit,'cy6') 
 condition gives me the output i need if subject has visit=cy6.

 

But for subject Id=3 does not have visit=cy6. He has visit=cy9 after cy4. 

 

so if I use "if VISIT='cy6' or "index(visit,'cy6') >0" it is picking up visit=cy9.

 

 

I am including my link to original post:

Thank you

https://communities.sas.com/t5/forums/replypage/board-id/programming/message-id/116548 

Respected Advisor
Posts: 2,647

Re: How to use character value similar to numeric value

Posted in reply to knveraraju91

That's not the right link.

--
Paige Miller
Super Contributor
Posts: 307

Re: How to use character value similar to numeric value

Posted in reply to PaigeMiller
Respected Advisor
Posts: 2,647

Re: How to use character value similar to numeric value

Posted in reply to knveraraju91

I have looked at that thread, I really don't see a clear explanation in one place of what you want. Perhaps if I read every single comment and then combine them together in my mind, your requirements are clear, but really that's your job to make the requirements clear. So ... if you could, write a clear statement of the requirements you have ...

--
Paige Miller
Super Contributor
Posts: 307

Re: How to use character value similar to numeric value

Posted in reply to PaigeMiller

Thank you very much for your time.

 

specs:

 

First I have to sort data by id date visit.
second by id, I have look until "orres='pd'" or up to visit='cy6'
which ever comes earlier

 

Of the all the "orres values" till either "orres='pd'" or up to visit='cy6'


I have to pick best "orres" value by following (cr>pr>sd>pd>ne).Which means if there is 'cr' at any visit prior to either


"orres='pd'" or up to visit='cy6' then i have to pick that 'cr' over others.

 

If there are no orres='pd' and visit='cy6' then I have to pick best observed value by following (cr>pr>sd>pd>ne)

 

With my code the issue is, if any subject(ex: id=3) does not have visit=cy6  then visits after visit='cy6'  (eg: 'cy9' is appearing in my output. Thank you

Super Contributor
Posts: 307

Re: How to use character value similar to numeric value

Posted in reply to PaigeMiller

Hi

 

This sentence meaning

"If there are no orres='pd' and visit='cy6' then I have to pick best observed value by following (cr>pr>sd>pd>ne)"

 

assuming the patient discontinued before "visit=cy6"

Super Contributor
Posts: 307

Re: How to use character value similar to numeric value

Posted in reply to PaigeMiller

Hi

 

This sentence meaning

"If there are no orres='pd' and visit='cy6' then I have to pick best observed value by following (cr>pr>sd>pd>ne)"

 

assuming the patient discontinued before "visit=cy6". If there is visit in (cy7  or cy8 or cy9) I should not pick these.

 

Thank you

Super User
Posts: 13,006

Re: How to use character value similar to numeric value

Posted in reply to knveraraju91

@knveraraju91 wrote:

Hi

 

This sentence meaning

"If there are no orres='pd' and visit='cy6' then I have to pick best observed value by following (cr>pr>sd>pd>ne)"

 

assuming the patient discontinued before "visit=cy6". If there is visit in (cy7  or cy8 or cy9) I should not pick these.

 

Thank you


Do these values of visit have ANY role in the selection process: (cy7  or cy8 or cy9).

If not then exclude them entirely such as:

proc sort data=ONE out=SORTED(index=(ID_ORRES=(ID ORRES)));
  where visit not in ('cy7' 'cy8' 'cy9');
  by ID DATE  VISIT;
run;

If they do play any role then the details need to be shared.

 

Super User
Posts: 22,820

Re: How to use character value similar to numeric value

Posted in reply to knveraraju91

@knveraraju91 It may help to link to your original question.

Ask a Question
Discussion stats
  • 10 replies
  • 96 views
  • 1 like
  • 4 in conversation