BookmarkSubscribeRSS Feed
393310
Obsidian | Level 7
 
6 REPLIES 6
ballardw
Super User

Your OUTPUT in the middle of the do loop forces SAS to write to the output data set for each iteration of the  K loop.

 

Suggestion for readable code: do not place things like Array definitions or similar inside the loops. It makes it harder to follow the code.

 

Likely you want a structure like this if you want all the calculations in the K loop written once. Order of statements does matter.

do i= 1 to limit;
   do k=1 to klimit;
    <stuff>
  end; /* k loop*/
  output;
end; /*i loop*/
393310
Obsidian | Level 7

Hi Thanks fo

 

 

 

 

 

ballardw
Super User

Here's a silly idea, provide a small example of data, maybe only 6 variables with about 5 rows of data.

Show what you expect the output to look like. NOT as pictures:

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

 

There is not place in your code where you assign a value to either Trouble_sleeping0 or Newvariables[1]. So I am not sure where you think 0, 1 come from.

 

I submit that when you show a variable with values that match the NAME of a variable and then show a want of something completely different that you have the wrong assignment and possibly function

Tom
Super User Tom
Super User

What is the question here?

If sounds like you have instrument that is measuring multiple features (Anxiety, Depression, Trouble_Sleeping etc.) that you are getting result for multiple times.  How are the answers to those questions stored?  Are they numbers?  Perhaps something like "on a scale from 1 to 10 how Anxious have you been in the last week? with 1 being none and 10 by extremely"

 

Do you want to check for changes in each of the individual items?  For example increase in Anxiety?

Perhaps something as simple as:

data want;
  set have;
  by id visit ;
  anxiety_change = sign( dif(anxiety) );
  if first.id then anxiety_change=0;
run;

Which will result is values of ANXIETY_CHANGE on each observation of 0 (no change), 1 (increase), or -1 (decrease).

 

Or do you want something more complicated? 

 

Is there any need to convert the value on the multiple individual questions into a single scale?  If so what is the formula?  And do you want to asses the change in that aggregate scale over time also?

 

 

393310
Obsidian | Level 7

I think that is on track for what I want. The variables are originally a numeric likert scale (anxiety is just 0 or 1) but the others in the data set are 0-5. At this point my data looks something like this: 

 

record idvisit numberoutcome(formatted values)noformat(unformatted values of the outcomes)N(number of times the same level of a variable was recorded)
11Anxiety present11
12Anxiety not present01
13Anxiety not present02

 

I am trying to create a code that can compare the values between visit 1 and visit2-all visits. So I want to say if patient 1's noformat value was 0 at visits 2-allvisits (in this case 2-3) and noformat=1 at visit1 then X='improvement'. Basically to be categorized as improved the score has to improve and stay improved at all follow up visits. I am struggling because I don't know what the syntax would be for something like this. 

 

Is there a way to do this with a long data set or do I have to transpose the data into a wide format?

 

Tom
Super User Tom
Super User

Figure out how to do ONE variable first.  To then apply the same logic to multiple variables there are two choices.  ) Use arrays and looping over the arrays to apply the same logic to multiple variables . 2) Transposing your data to actual TALL structure (not the intermediate structure you are starting with) and use the additional resulting variable as a BY variable.

 

Sounds like you first want to calculate a change from baseline measure.  Then can try to access your criteria of sustained improvement to the end.   Do you just want one number of each subject?  If so how would its result be different if you just took the change from baseline for the last visit?

 

Here is a simple way to do a calculate change from baseline that will work if your data is sorted by subject and visit.  Say your identifier variable is named ID, your visit variable is named VISIT and your numeric variable is named VALUE.  First calculate a BASELINE value and then use the to compare to the following visit values to see if there is improvement.  SAS will evaluate a boolean expression as 1 for TRUE and 0 for FALSE.

data want;
  set have ;
  by id visit;
  if first.id then baseline = value ;
  else improvement = (value>baseline) ;
  retain baseline;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 862 views
  • 0 likes
  • 3 in conversation