BookmarkSubscribeRSS Feed
kryden
Calcite | Level 5

Hey all,

 

So I have some output from PROC MEANS where I have some basic values spread across four visits.  The first visit is considered the baseline visit.

I am trying to crate a dataset that contains the difference from baseline for each variable at each visit:

example.png

Anyone have any thoughts on how I can do this?

 

2 REPLIES 2
art297
Opal | Level 21

Here is one way:

data have;
informat visit $25.;
input visit & n Mean SD Med Q1 Q3 Min Max;
cards;
Baseline Visit 48 33.8 9.6 32.6 26.2 38.1 18.6 59.7
6 Week Follow-up 35 37.8 7.1 38.1 31.2 42.2 22.8 52.5
3 Month Follow-up 25 42.3 8 42.4 37.3 47.8 18.4 56.4
6 Month Follow-up 2 54.5 1.5 54.5 53.5 55.6 53.5 55.6
;

data want (drop=_:);
set have;
retain _Mean _SD _Med _Q1 _Q3 _Min _Max;
if _n_ eq 1 then do;
_Mean=Mean;
_SD=SD;
_Med=Med;
_Q1=Q1;
_Q3=Q3;
_Min=Min;
_Max=Max;
end;
else do;
Mean+-_Mean;
SD+-_SD;
Med+-_Med;
Q1+-_Q1;
Q3+-_Q3;
Min+-_Min;
Max+-_Max;
output;
end;
run;

Art, CEO, AnalystFinder.com

 

Note: Corrected to drop retained variables

mkeintz
PROC Star

This is an alternative to @art297's suggestion.  It takes advantage of the DIF function (where dif(x) is defined as x-lag(x)), and interspersing a baseline observation between each of the other visits:

 

 

data have;
 informat visit $25.;
 input visit & n Mean SD Med Q1 Q3 Min Max;
 cards;
Baseline Visit           48 33.8 9.6 32.6 26.2 38.1 18.6 59.7
6 Week Follow-up         35 37.8 7.1 38.1 31.2 42.2 22.8 52.5
3 Month Follow-up        25 42.3 8 42.4 37.3 47.8 18.4 56.4
6 Month Follow-up        2 54.5 1.5 54.5 53.5 55.6 53.5 55.6
run;


data want (drop=i);
  set have (where=(visit=:'B'))
      have (where=(visit=:'6 Week') in=in6w)
      have (where=(visit=:'B'))
      have (where=(visit=:'3 M') in=in3m)
      have (where=(visit=:'B'))
      have (where=(visit=:'6 M') in=in6m);
  array x {*} mean--max;
  do i=1 to dim(x);
    x{i}=dif(x{i}); 
  end;
  if in6w or in3m or in6m;
run;

 

The SET statement interleaves 6 subsets of HAVE, where one subset (visit=:'B') is read 3 times, once preceding the visit=:'6 W', once before visit=:'3 M' and once preceding '6 M'.  This means the DIF function, which calculates x-lag(X), will be generating

     X at 6 weeks minus X at baseline

     X at 3 months minus X at baseline
     X at 6 months minus X at baseline

 

The   "where" parameters use the operator  "=:", which says to take the shorter string and compare it only to the start of the longer string.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

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.

 

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.

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
  • 2 replies
  • 1373 views
  • 3 likes
  • 3 in conversation