DATA Step, Macro, Functions and more

i have a datastep which i am using to calculate withdrawals but i need to set condition

Reply
Regular Contributor
Posts: 214

i have a datastep which i am using to calculate withdrawals but i need to set condition

[ Edited ]

i have a datastep which i am using to calculate withdrawals but i need to set condition by using another dataset which has multiple reasons for withdrawals. Can i do that in same datastep? any help.

data attached:

datastep used is : the one which is bold should be overwritten with the other dataset(_adds) which is attached. How can i take this new dataset by using any condition in the below dataset

 

data work.Disp_Withdraw_w2;
	set work.Disp_Withdraw_w1;
	length desc $50. dsreas_fmt $50. sort1 sort2 8.;

	if compress(actarm) = "BLINDED" then
		do;
			desc = vlabel(SAFFL);
			dsreas_num_BLINDED = 1;
			dsreas_fmt = "Number of Patients";
			sort1 = 1;
			sort2 = 0;
			output;

			if SAFFL='Y' then
				do;
					if EOTSTT = 'Completed' then
						do;
							dsreas_num_BLINDED = 1;
							dsreas_fmt = " Completed";
							sort2 = 1;
							output;
						end;
					else if EOTSTT ='Discontinued' then
						do;
							dsreas_num_BLINDED = 1;
							dsreas_fmt = " Withdrawn";
							sort2 = 2;
							output;
						end;
				end;

			if PDCTREAS ne "" then
				do;
					desc= "ByPrim";
					dsreas_num_BLINDED = 1;
					dsreas_fmt = " "||PDCTREAS;
					sort1 = 88;
					output;
				end;
		end;

	if compress(actarm) = "PENDING" then
		do;
			desc = vlabel(SAFFL);
			dsreas_num_PENDING = 1;
			dsreas_fmt = "Number of Patients";
			sort1 = 1;
			sort2 = 0;
			output;

			if SAFFL='Y' then
				do;
					if EOTSTT = 'Completed' then
						do;
							dsreas_num_PENDING = 1;
							dsreas_fmt = " Completed";
							sort2 = 1;
							output;
						end;
					else if EOTSTT ='Discontinued' then
						do;
							dsreas_num_PENDING = 1;
							dsreas_fmt = " Withdrawn";
							sort2 = 2;
							output;
						end;
				end;

			if PDCTREAS ne "" then
				do;
					desc= "ByPrim";
					dsreas_num_PENDING = 1;
					dsreas_fmt = " "||PDCTREAS;
					sort1 = 88;
					output;
				end;
		end;
run;

 

Attachment
Super User
Super User
Posts: 9,599

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

I believe this has been mentioned on all your other posts:

PleAsE pOST tEst data IN tHE Form oF a dAtAstep iN the body of YoUR post, usINg the {i} code WinDOW.

Note the above is done deliberately to show how hard it is to read code which has no formatting issues, has no casing consistency etc.

data work.disp_withdraw_w2;
  set work.disp_withdraw_w1;
  length desc $50. dsreas_fmt $50. sort1 sort2 8.;
  if compress(actarm)="BLINDED" then do;
    desc = vlabel(saffl);
    dsreas_num_blinded=1;
    dsreas_fmt="Number of Patients";
    sort1=1;
    sort2=0;
    output;
    if saffl='Y' then do;
      if eotstt='Completed' then do;
        dsreas_num_blinded=1;
        dsreas_fmt=" Completed";
        sort2=1;
        output;
      end;
      ...
    end;
    ...
  end;
run;

Now to your issue, without seeing the data its hard to say, does the data match in any way, i.e. can you merge the two together based on some condition, if so then do that, and in your if just use the variable merged on from the second dataset, then drop all variables from the second dataset.  If you can't merge the two, how does the look up work?  You may have to use some sort of code generation from the rules dataset to generate the other.

Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Sorry for that. I had indented the code but while pasting it got back to normal. extremely sorry for that.

attaching the 2 datasets. i tried to merge but in one dataset there is one record per observation and the other has multiple as it contains multiple reasons for withdrawal. i tried to transpose the second one which has multiple records and merge by subjid but then it creates multiple observations.

 

Attachment
Attachment
Super User
Super User
Posts: 9,599

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

I would say that merging is probably best here.  Got a meeting so this is only quick, but setup the one with dups as:

data adds_inter (keep=subjid list);
  set adds;
  length list $2000;
  retain list;
  by subjid;
  if first.subjid then list="";
  list=catx(',',list,avalc);
  if last.subjid then output;
run;

This should give you one row per subject which you can then merge onto the other dataset and use in your if.

Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Thanks, but i need to have summary i.e number of withdrawals reasons.

I am making a table with one option as primary reason for withdrawal and another option with all reasons

 

 

Super User
Super User
Posts: 9,599

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Is that a summary including each of the reasons from the other dataset?  Not sure how its meant to look, if its just a count of the number in the other dataset:

data adds_inter (keep=subjid cnt);
  set adds;
  retain cnt;
  by subjid;
  cnt=ifn(first.subjid,0cnt+1);
  if last.subjid then output;
run;

Basically the idea is to get your data as you need it for the output, so manipulate it into a 1 row structure.

Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Basically i need is an option which keeps either primar reason or all reasons which was there in other dataset.

 

Attached is the sample output for primary reason which i get now but i need to keep an option for all reasons from the other dataset.

and in other dataset it only has reasons for withdrawal.

 

Attachment
Super User
Super User
Posts: 9,599

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Yep, standard withdrawal table.  In which case I have seen two approaches:

1) In ADSL programming have a primary reason variable.

2) Define in the dataset where reasons are kept, which consititutes "primary" and merge that on.  Alternatively if they are all to be there, summarise that dataset, then append to your other dataset.  I.e. don't do it all in one step. 

 

 

Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

[ Edited ]

Thanks a lot, I transposed the other dataset and merged it.

i did the below after transpose. when i use the below code it gives error as &i resolves to numeric value

NOTE: Invalid numeric data, SECREAS1='Withdrawal of consent' , at line 1 column 2.

is there anyother way to do this.

 

         %if %upcase(&RptReas.) = ALL %then %do;

               	desc = "All";
				dsreas_num_%scan(&ArmList.,&k., #)=%scan(&ReasLst., &i.);
				if dsreas_num_%scan(&ArmList.,&k., #) then do;
					dsreas_fmt = "    "||&i.;
					sort2 = &i.;
					output;
				end;
			%end;

it got resolved to 

data work.Disp_Withdraw_w21;
   set work.Disp_Withdraw_w2;
   if compress(actarm) = "BLINDED" then do;
        desc = "All";
        dsreas_num_BLINDED = SECREAS1;
   if dsreas_num_BLINDED then do;
        dsreas_fmt = "    "||2;
        sort2 = 2;
        output;
    end;
    end;
    if compress(actarm) = "PENDING" then do;
        desc = "All";
        dsreas_num_PENDING = SECREAS1;
    if dsreas_num_PENDING then do;
        dsreas_fmt = "    "||2;
        sort2 = 2;
        output;
     end;
     end;
run;

 

Attachment
Super User
Super User
Posts: 9,599

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Macro vars are always text, so you can just do:

dsreas_fmt = "    "||&i.;

To

dsreas_fmt = "    &i.";

 

Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

i changed it but still it somehow resoles to numeric

dsreas_num_%scan(&ArmList.,&k., #)=%scan(&ReasLst., &i.);

 i have attached my test data earlier post.

 

NOTE: Invalid numeric data, SECREAS1='Withdrawal of consent' , at line 1 column 2.

Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

any help on it to fix

Super User
Super User
Posts: 9,599

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

/Sorry, the code you post:

dsreas_num_%scan(&ArmList.,&k., #)=%scan(&ReasLst., &i.);

Does not match anything else you posted before.  What is armlist, reaslist &i etc.  Look at what I have posted.  Get your data into a usable way, do datasteps to get it as you want then merge the data together, forget macros and other things this is basic data manipulations.   

Super User
Posts: 10,236

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

As you probably have been told several times by now, use either the "little running man" or {i} icons to post SAS code, logs or text data that must not be reformatted by the forum software. Posting code in the main window is NOT recommended.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 214

Re: i have a datastep which i am using to calculate withdrawals but i need to set condition

Posted in reply to KurtBremser
data work.Disp_Withdraw_w2;
        set work.Disp_Withdraw_w1;
        length desc $50. dsreas_fmt $50. sort1 sort2 8.;
     if compress(actarm) = "BLINDED" then do;
        	desc = vlabel(SAFFL);
        	dsreas_num_BLINDED = 1;
        	dsreas_fmt = "Number of Patients";
        	sort1 = 1;
        	sort2 = 0;
        	output;
        if SAFFL='Y' then do;
        	if EOTSTT = 'Completed' then do;
        	dsreas_num_BLINDED = 1;
        	dsreas_fmt = "   Completed";
        	sort2 = 1;
        	output;
        end;
        else if EOTSTT ='Discontinued' then do;
        	dsreas_num_BLINDED = 1;
        	dsreas_fmt = "   Withdrawn";
        	sort2 = 2;
        	output;
        end;
        end;
        if PDCTREAS ne "" then do;
        	desc= "ByPrim";
        	dsreas_num_BLINDED = 1;
        	dsreas_fmt = "    "||PDCTREAS;
        	sort1 = 88;
     	   output;
        end;
     end;
     if compress(actarm) = "PENDING" then do;
        	desc = vlabel(SAFFL);
        	dsreas_num_PENDING = 1;
        	dsreas_fmt = "Number of Patients";
        	sort1 = 1;
        	sort2 = 0;
        	output;
        if SAFFL='Y' then do;
        	if EOTSTT = 'Completed' then do;
        	dsreas_num_PENDING = 1;
        	dsreas_fmt = "   Completed";
       		sort2 = 1;
        	output;
        end;
        else if EOTSTT ='Discontinued' then do;
       		dsreas_num_PENDING = 1;
        	dsreas_fmt = "   Withdrawn";
        	sort2 = 2;
        	output;
        end;
        end;
        if PDCTREAS ne "" then do;
        	desc= "ByPrim";
        	dsreas_num_PENDING = 1;
        	dsreas_fmt = "    "||PDCTREAS;
        	sort1 = 88;
        	output;
        end;
     end;
run;
Ask a Question
Discussion stats
  • 14 replies
  • 293 views
  • 0 likes
  • 3 in conversation