BookmarkSubscribeRSS Feed
SAS-PD
Fluorite | Level 6

Hello,

I have 13 different labs for each subject; I need to list all lab test name as a list for each where value is 3


data Lab;
infile datalines;
input Subjid $ Lab1 Lab2 Lab3 Lab4 Lab5 Lab6 Lab7 Lab8 Lab9 Lab10 Lab11 Lab12 Lab13 ;
datalines;
Subj1 1 3 3 1 1 1 1 2 . 1 1 1 1
Subj2 1 1 1 1 1 1 1 1 1 1 1 . 3
;
run;

 

Get records with Any Lab from 1 to 13 has value 3

data lab_1;
set Lab ;
where (Lab1 = 3 OR
Lab2 = 3 OR
Lab3 = 3 OR
Lab4 = 3 OR
Lab5 = 3 OR
Lab6 = 3 OR
Lab7 = 3 OR
Lab8 = 3 OR
Lab9 = 3 OR
Lab10 = 3 OR
Lab11 = 3 OR
Lab12 = 3 OR
Lab13 = 3 );
run;

 

Need help to display Lab Name as separate records if value = 3

Data Want;

Subject     Lab Name
----------------------
Subj1            Lab2
Subj1           Lab3
Subj2          Lab13

 

Thanks,

 

5 REPLIES 5
ballardw
Super User

If SUBJID is not repeated:

 

proc transpose data=lab out=labtrans;
   by subjid;
   var lab: ;
run;

proc print data=labtrans noobs label;
  where col1=3;
  var subjid _name_;
  label _name_ ='Lab name';
run;

You would have to sort the Lab data set by Subjid if it is not in that order OR use the Notsorted option on the BY statement in the Proc Transpose step.

 

You will find that for many processes long beats wide. That means one observation per similar measurements (per lab in this case) with a variable holding where the measure comes from (the lab name).

Proc Transpose is one tool that may get to that data structure.

SAS-PD
Fluorite | Level 6

Hello, Thank you!! Proc transpose worked 🙂 just got the request updated-- I would like to get all other variables present in the dataset like result, unit, range etc. along with the SubjectID and test name. All corresponding lab variables are labeled as Result1-13, Unit1-13, Range1-13 like Lab1-13


data Lab;
infile datalines;
input Subjid $ Lab1 Lab2 Lab3 Lab4 Lab5 Lab6 Lab7 Lab8 Lab9 Lab10 Lab11 Lab12 Lab13 Result1 Result2 Result3 Result4 Result5 Result6 Result7 Result8 Result9 Result10 Result11 Result12 Result13 ;
datalines;
Subj1 1 3 3 1 1 1 1 2 . 1 1 1 1 01 02 03 04 05 06 07 08 . 010 011 012 013
Subj2 1 1 1 1 1 1 1 1 1 1 1 . 3 011 012 013 014 015 016 017 018 019 0110 0111 . 0113
;
run;

Get ALL respective variables like result, unit, range etc. for the records with Any Lab from 1 to 13 has value 3

 

Data Want;

Subject Lab Name Lab Result Lab Units etc..
--------------------------------------------------
Subj1 Lab2            02
Subj1 Lab3             03
Subj2 Lab13           0113

mkeintz
PROC Star

@SAS-PD wrote:

Hello, Thank you!! Proc transpose worked 🙂 just got the request updated-- I would like to get all other variables present in the dataset like result, unit, range etc. along with the SubjectID and test name. All corresponding lab variables are labeled as Result1-13, Unit1-13, Range1-13 like Lab1-13


data Lab;
infile datalines;
input Subjid $ Lab1 Lab2 Lab3 Lab4 Lab5 Lab6 Lab7 Lab8 Lab9 Lab10 Lab11 Lab12 Lab13 Result1 Result2 Result3 Result4 Result5 Result6 Result7 Result8 Result9 Result10 Result11 Result12 Result13 ;
datalines;
Subj1 1 3 3 1 1 1 1 2 . 1 1 1 1 01 02 03 04 05 06 07 08 . 010 011 012 013
Subj2 1 1 1 1 1 1 1 1 1 1 1 . 3 011 012 013 014 015 016 017 018 019 0110 0111 . 0113
;
run;

Get ALL respective variables like result, unit, range etc. for the records with Any Lab from 1 to 13 has value 3

 

Data Want;

Subject Lab Name Lab Result Lab Units etc..
--------------------------------------------------
Subj1 Lab2            02
Subj1 Lab3             03
Subj2 Lab13           0113


Then make a second array for result1-result13 to parallel lab1-lab13:

 

data want (keep=subjid labname result);
  set lab;
  array labs  lab1-lab13 ;
  array rslts result1-result13;
  do over labs;
    if labs=3 then do;
      labname=vname(labs);
      result=rslts;
      output;
    end;
  end;
run;
--------------------------
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

--------------------------
Tom
Super User Tom
Super User

It would be much easier if you just read it in that way to begin with.

data Lab;
  input Subjid $ @;
  do lab=1 to 13 ;
    input result @ ;
    output;
  end;
datalines;
Subj1 1 3 3 1 1 1 1 2 . 1 1 1 1
Subj2 1 1 1 1 1 1 1 1 1 1 1 . 3
;

Then a simple WHERE statement will suffice.

proc print data=lab;
 where result=3;
run;

Result

Obs    Subjid    lab    result

  2    Subj1       2       3
  3    Subj1       3       3
 26    Subj2      13       3

mkeintz
PROC Star

You can use the VNAME function:

 

data Lab;
infile datalines;
input Subjid $ Lab1 Lab2 Lab3 Lab4 Lab5 Lab6 Lab7 Lab8 Lab9 Lab10 Lab11 Lab12 Lab13 ;
datalines;
Subj1 1 3 3 1 1 1 1 2 . 1 1 1 1
Subj2 1 1 1 1 1 1 1 1 1 1 1 . 3
;
run;

data want (keep=subjid labname);
  set lab;
  array labs  lab1-lab13 ;
  do over labs;
    if labs=3 then do;
      labname=vname(labs);
      output;
    end;
  end;
run;
--------------------------
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 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
  • 5 replies
  • 592 views
  • 3 likes
  • 4 in conversation