SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

Hi, i'm an italian biostatistic student and right now i'm using sas for my final project.

What i'm trying to do is using do loops to see if patient from my data set are respecting the follow up visit periodicity. Let me be more clear: as written in the study protocol, after hospitalization, each patient has the first follow up visit after three months and the following after 4 months each. Obviously this periodicity was not respected by all patient. So i'm trying to place  in time the visits that i have for each patient.

Let's say for example that a patient showed up oly for 4 visit: first two are hospitalization and remission, and the other two are the follow up visit.

 

27.PNG

 

Using do loops i'm able to correctly assign, based on the month difference between each visit date and the remission date, each follow up visit to its corresponding step (1: Hospitalization; 2: Remission; 3: first visit; 4: Second visit; 5 third ...and so on). Also if a visit don't match with one step i want to output the row anyway with missing values.

 

The code i'm using is the following:

 

data fuobauxo_checked;
	retain id visit step;
	set fuobauxo_check;
	by id;
	
	if visit >= 3 then month = round((data - remission)/30, 1); 
	*intck('month', remission, data);
	retain j;
	if first.id then j = 2; 

	if visit ne 1 and visit ne 2 then 
	do;
	/*Il ciclo ogni volta riparte da capo. Come faccio a far si che il contatore parta dal valore dell'ultima iterazione? */
		do i = 1 to 70 by 4;
			if i < month <= (i+4) then 
			do;
				month_visit = i+2;				
				j = j + 1;
				step = j;
				output; 
				leave;
			end;
			else 
			do;
				j = j + 1;
				step = j;
				output; 
			end;
		end;
	end;
	else 
		do;
			if visit = 1 then do; step = 1; month_visit = 0; output; end;
			if visit = 2 then do; step = 2; month_visit = 1; output; end;
		end;
	
	if id = 1098 then delete;
	drop remission;
run;

 

The result is the following:

 

27_bis.PNG

 

The problem is that if a visit is classified correctly the loop is interrupted and when the datastep procede to read the next visit the loop restart from the beginning comparing the new visit with every single possible step until a correspondence is found.

So, my question is: there is a way to keep in memory the value of the do loop counter in a way that when the datastep read the next row the loop restart with the value that the counter had a the end of the previous loop?

 

I hope everything it's clear and I thank you for every suggestion!

PS: sorry if my english is not perfect!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

It's really hard to answer such a question without sample data and some code matching the sample data. 

So just guessing here and proposing something untested: Could you eventually retain the the counter value (r_i) and then start in the next row for the same id the loop at the value of the retained value? If so then something like below might work.

data _null_;
  set <your table>;
  by id;
  retain r_i;
  if first.id then r_i=1;
  do i=r_i to 70 by 4;
    r_i=i+1;


  end;
run;

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20

To reply to your specific question:

Yes, I guess that you could capture the loop variable value (i), by assigning to another retained variable.

And then you need to add an IF statement that will omit your do loop the execute anything until it reaches the desired loop variable value.Haven't looked into your code in detail, bu do loop are usually use soley within an observation, so there might be a more appropriate way to sole your problem.

Data never sleeps
Patrick
Opal | Level 21

It's really hard to answer such a question without sample data and some code matching the sample data. 

So just guessing here and proposing something untested: Could you eventually retain the the counter value (r_i) and then start in the next row for the same id the loop at the value of the retained value? If so then something like below might work.

data _null_;
  set <your table>;
  by id;
  retain r_i;
  if first.id then r_i=1;
  do i=r_i to 70 by 4;
    r_i=i+1;


  end;
run;
Samuele_Minari
Calcite | Level 5
I've alredy tried this method but it hasn't work but I will surely try again.
Thanks a lot for your suggestion!!
Patrick
Opal | Level 21

@Samuele_Minari wrote:
I've alredy tried this method but it hasn't work but I will surely try again.
Thanks a lot for your suggestion!!

Make sure that the retained counter variable gets a value of <counter variable> + 1 assigned before you leave the loop.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1230 views
  • 2 likes
  • 3 in conversation