BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I am sorry , If my title is confusing. I am came up with the title best of my ability.  I am looking to create an additional record easy way  based on the data available.

For example:  In my data,  subjid =101 and Visit= V2, missing the  PARAMCD= BMI and same applies to the subjid=103 at visit= V1.

So I am looking is there any easy way, I can check my subjects  missing PARAMCD='BMI' at a particular visit and if it missing at a particular Visit, I need to calculate the BMI ( BMI= wit/(hit*hit))?

data vital;
input subjid visit$ paramcd$ aval ;
cards;
101 V1 hit 1.60 
101 V1 wit 80 
101 V1 BMI 31.2
101 V2 hit 1.60 
101 V2 wit 90 
103 V1 hit 1.502 
103 V1 wit 74 
103 V2 hit 1.502 
103 V2 wit 75 
103 V2 BMI 33.2 
;
RUN;

SASuserlot_0-1663017337564.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

This task can be solved with a single data step, but you need to be sure that BMI is always the last obs in each visit. Unfortunately i don't see a way to ensure this by code.

data want_one;
   set sorted;
   by subjid visit;

   retain _hit _wit;

   if first.visit then do;
      call missing(_hit, _wit);
   end;

   select (lowcase(paramcd));
      when ('hit') _hit = aval;
      when ('wit') _wit = aval;
      otherwise;
   end;

   output;

   if last.visit and lowcase(paramcd) ^= 'bmi' then do;
      paramcd = 'BMI';
      aval = _wit / (_hit * _hit);
      output;
   end;

   drop _:;
run;

Another idea, with longer runtime:

proc transpose data=sorted out=transposed(drop=_name_);
   by subjid visit;
   var aval;
   id paramcd;
run;

data filled;
   set transposed;

   if missing(bmi) then do;
      bmi = wit / (hit * hit);
   end;
run;

proc transpose data=filled out=want_transpose(rename= (col1 = aval)) name=paramcd;
   by subjid visit;
   var hit wit bmi;
run;

View solution in original post

8 REPLIES 8
andreas_lds
Jade | Level 19

This task can be solved with a single data step, but you need to be sure that BMI is always the last obs in each visit. Unfortunately i don't see a way to ensure this by code.

data want_one;
   set sorted;
   by subjid visit;

   retain _hit _wit;

   if first.visit then do;
      call missing(_hit, _wit);
   end;

   select (lowcase(paramcd));
      when ('hit') _hit = aval;
      when ('wit') _wit = aval;
      otherwise;
   end;

   output;

   if last.visit and lowcase(paramcd) ^= 'bmi' then do;
      paramcd = 'BMI';
      aval = _wit / (_hit * _hit);
      output;
   end;

   drop _:;
run;

Another idea, with longer runtime:

proc transpose data=sorted out=transposed(drop=_name_);
   by subjid visit;
   var aval;
   id paramcd;
run;

data filled;
   set transposed;

   if missing(bmi) then do;
      bmi = wit / (hit * hit);
   end;
run;

proc transpose data=filled out=want_transpose(rename= (col1 = aval)) name=paramcd;
   by subjid visit;
   var hit wit bmi;
run;
SASuserlot
Barite | Level 11

Thank you @andreas_lds . It worked and do the job I wanted.  Thank you for pointing out on BMI being the last in Each Visit.  I can assign the Numeric numbers to the paramcds and Make sure it is always at the last. 

SASuserlot
Barite | Level 11

Good question.  For my actual data the answer is 'Yes' because we only keeping the records that have the both at a particular visit.

Ksharp
Super User
data vital;
input subjid visit$ paramcd$ aval ;
cards;
101 V1 hit 1.60 
101 V1 wit 80 
101 V1 BMI 31.2
101 V2 hit 1.60 
101 V2 wit 90 
103 V1 hit 1.502 
103 V1 wit 74 
103 V2 hit 1.502 
103 V2 wit 75 
103 V2 BMI 33.2 
;
RUN;

proc sql;
create table want as
select a.*,b.aval as aval
 from (select * from
(select distinct subjid from vital),
(select distinct visit from vital),
(select distinct paramcd from vital)
) as a
natural left join vital as b;
quit;
SASuserlot
Barite | Level 11

Thank you for your suggestion @Ksharp .  I think you missing to Include the BMI calculation in the code, I am getting missing values ( AVAL)  for the Records (BMI) Newly created. and I also see a note in the log, Is it gonna run into any issues?

NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.

SASuserlot_0-1663255812419.png

 

Ksharp
Super User

After getting WANT dataset ,it is very easy to get BMI.

 

data vital;
input subjid visit$ paramcd$ aval ;
cards;
101 V1 hit 1.60 
101 V1 wit 80 
101 V1 BMI 31.2
101 V2 hit 1.60 
101 V2 wit 90 
103 V1 hit 1.502 
103 V1 wit 74 
103 V2 hit 1.502 
103 V2 wit 75 
103 V2 BMI 33.2 
;
RUN;

proc sql;
create table want as
select a.*,b.aval as aval
 from (select * from
(select distinct subjid from vital),
(select distinct visit from vital),
(select distinct paramcd from vital)
) as a
natural left join vital as b;
quit;
data want;
 merge want want(keep=aval rename=(aval=hit) firstobs=2 ) want(keep=aval rename=(aval=wit) firstobs=3 );
 if paramcd='BMI' and missing(aval) then aval=wit/hit**2;
 drop hit wit ;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 8 replies
  • 1743 views
  • 4 likes
  • 4 in conversation