BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bkq32
Quartz | Level 8

I have a table where each biomarker has a corresponding flag variable that equals 1 if it needs to be reviewed. Is there a way to systematically output the SUBJECTID and the biomarker name and value if the variable is flagged?

 

data have;
 input SUBJECTID $ BLD_GGT BLD_TRIG BLD_INS BLD_GGT_FLAG BLD_TRIG_FLAG BLD_INS_FLAG;
cards;
001 12 100 43 0 0 1
002 12 99 42 0 0 0
003 15 104 30 0 0 0
004 3  109 24 1 1 0
005 14 100 23 0 0 0 
;
run;
data want;
input SUBJECTID $ VARIABLE $ VALUE;
cards;
001 BLD_INS 43
004 BLD_GGT 15
004 BLD_TRIG 109
;
run; 
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Try next code:

data want;
 set have;
     length variable $15; /* addapt to max variable name length */
     array flg BLD_GGT_FLAG BLD_TRIG_FLAG BLD_INS_FLAG;
	 array bio BLD_GGT      BLD_TRIG      BLD_INS;
	 do i=1 to dim(flg);
	    if flg(i)=1 then do;
		   variable = vname(bio(i));
		   value = bio(i);
		   output;
		end;
	 end;
	 keep subjectid variable value;
run;

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

Hi @bkq32  Methinks your HAVE has value ambiguity or a typo. So i modified your HAVE with that assumption

data have;
 input SUBJECTID $ BLD_GGT BLD_TRIG BLD_INS BLD_GGT_FLAG BLD_TRIG_FLAG BLD_INS_FLAG;
cards;
001 12 100 43 0 0 1
002 12 99 42 0 0 0
003 15 104 30 1 0 0
004 3  109 24 0 1 0
005 14 100 23 0 0 0 
;
run;

data want;
 set have;
 array b  BLD_GGT BLD_TRIG BLD_INS;
 array f BLD_GGT_FLAG BLD_TRIG_FLAG BLD_INS_FLAG;
 _n_=whichn(1,of f(*));
 if _n_;
 variable=vname(b(_n_));
 value=b(_n_);
 keep subjectid variable value;
run;
bkq32
Quartz | Level 8

Hi @novinosrin, The original HAVE table was correct since some records will have more than one flag, in which case I'd like to create separate records for them in WANT with the variable name and value for each of those biomarkers. Is there a way to do that?

novinosrin
Tourmaline | Level 20

Hi @bkq32  Alright. Can you explain how this result qualifies ?

004 BLD_GGT 15

 

bkq32
Quartz | Level 8

@novinosrin A clinician might've told me to run something like:

if BLD_GGT>14 then BLD_GGT_FLAG=1;
Shmuel
Garnet | Level 18

Try next code:

data want;
 set have;
     length variable $15; /* addapt to max variable name length */
     array flg BLD_GGT_FLAG BLD_TRIG_FLAG BLD_INS_FLAG;
	 array bio BLD_GGT      BLD_TRIG      BLD_INS;
	 do i=1 to dim(flg);
	    if flg(i)=1 then do;
		   variable = vname(bio(i));
		   value = bio(i);
		   output;
		end;
	 end;
	 keep subjectid variable value;
run;
bkq32
Quartz | Level 8

Thank you, @Shmuel. When I tried running that code, I go this error:

 

411 data want;
412 set have;
413 length variable $15; /* addapt to max variable name
413! length */
414 array flg BLD_GGT_FLAG BLD_TRIG_FLAG BLD_INS_FLAG;
415 array bio BLD_GGT BLD_TRIG BLD_INS;
416 do i=1 to dim(flgs);
ERROR: The DIM, LBOUND, and HBOUND functions require an array
name for the first argument.
417 if flg(i)=1 then do;
418 variable = vname(bio(i));
419 value = bio(i);
420 output;
421 end;
422 end;
423 keep subjectid variable value;
424 run;

Shmuel
Garnet | Level 18

The array name is flg not flgs - tat was my typo;

change line to

do i=1 to dim(flg);

bkq32
Quartz | Level 8
Thank you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 878 views
  • 0 likes
  • 3 in conversation