DATA Step, Macro, Functions and more

Syntax Help

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Syntax Help

[ Edited ]

Hello to all,
I have a dataset with the variables concerning the blood count.
I have dichotomized the variables in 0.1.
how do I do if I have a sex-dependent variable?
wbc for women (sex = 0) its range is from 370 to 500 the values out of the range become 0 inside equal to 1, for males (sex = 1) their range goes from 410 to 540 values out of the range become o and inside 1.
should the output be 0 and 1 in a single variable?

I tried in the second part of the eg.

 

eg 1

 

data datasetdummy;
	set WORK.PPP;

	if Sesso='F' then
		Sesso=0;
	else
		Sesso=1;
	*nuova variabile binaria riguardante il sesso l'ho chiamata sesso (0=donna,1=uomo);

	if Interno='SI' then
		Interno=0;
	else
		Interno=1;
	* interno (si=0,no=1);

	if NEUT <=90 then
		NEUT=0;
	else if NEUT>=450 then
		NEUT=0;
	else
		NEUT=1;
	*se NEUT non si trova tra 90-450 è uguale a 0 altrimenti è uguale a 1;

	if LYMPH <=100 then
		LYMPH=0;
	else if LYMPH>=300 then
		LYMPH=0;
	else
		LYMPH=1;
	*uguale al commento sopra per tutte le variabili MONO EO BASO;

	if MONO <=0 then
		MONO=0;
	else if MONO >=100 then
		MONO=0;
	else
		MONO=1;

	if EO <=0 then
		EO=0;
	else if EO>=40 then
		EO=0;
	else
		EO=1;

	if BASO <=0 then
		BASO=0;
	else if BASO>=5 then
		BASO=0;
	else
		BASO=1;

	if PLT<=130 then
		PLT=0;
	else if PLT>=330 then
		PLT=0;
	else
		PLT=1;

	if MCV<=800 then
		MCV=0;
	else if MCV>=970 then
		MCV=0;
	else
		MCV=1;

	if MCH <=270 then
		MCH=0;
	else if MCH>=340 then
		MCH=0;
	else
		MCH=1;

	if MCHC<=330 then
		MCHC=0;
	else if MCHC>=364 then
		MCHC=0;
	else
		MCHC=1;

	if WBC<=2600 then
		WBC=0;
	else if WBC>=7800 then
		WBC=0;
	else
		WBC=1;
run;

 

eg2

 

data dsd;
	set datasetdummy;

	if RBC.Sesso=0<=2600 then
		RBC=0;
	else if RBC.Sesso=0>=7800;
else
	RBC.Sesso=0=1;

if RBC.Sesso=1<=2600 then
	RBC=0;
else if RBC.Sesso=1>=7800;
else
	RBC.Sesso=1=1;
run;

 

 

 does anyone know how to implement it? I tried this way !!


Accepted Solutions
Solution
‎01-04-2018 11:28 AM
Super User
Posts: 13,084

Re: Syntax Help

Posted in reply to michshoot

michshoot wrote:

Hello to all,

 

how do I do if I have a sex-dependent variable?
wbc for women (sex = 0) its range is from 370 to 500 the values out of the range become 0 inside equal to 1, for males (sex = 1) their range goes from 410 to 540 values out of the range become o and inside 1.

should the output be 0 and 1 in a single variable?

I tried in the second part of the eg.

 


1. When recoding I generally place the values into a new variable so that errors of coding are easy to debug.

2. Often with ranges such as this I create custom formats as then I do not need to modify the data at all and most analysis procedures will use the formatted values as categories UNLESS I have an instance such as you have where the ranges differ by another characteristic such as gender or age.

 

You can use logic results to assign 0, 1 such as

 

   if sex=0 then do;
      wbc = (370 le wbc le 500);
   end;
   else do;
      wbc = (410 le wbc le 540);
   end;

SAS will assign a value of 1 to "true"

 

View solution in original post


All Replies
Super User
Super User
Posts: 9,227

Re: Syntax Help

Posted in reply to michshoot

Its really hard to read that code.  First, use the code window - its the {i} above post are, to post code as this retains formatting.  Second use some code formatting, i.e. not all mixed case, use indentations etc. Also, provide some test data, otherwise we are just guessing. 

 

From what I can tell here you have variables like NEU, LYMPH etc.  Can I ask why?  It is pretty much standard now in clinical trials to use CDISC models for the data, including this lab data, so why is it not in LB or ADLB format?

 

There are of course numerous ways to approach this, formats for instance with an if to format for male or female.  You could build it all as nestled ifs, e.g:

...
  if sex="Male" then do;
    neut=ifn(90 < neut  < 450,0,1);
    ...
  end;
  else do;
    neut=...

  end;
...

Or you can go the route of creating normal ranges and merging these on.  This would be the most normal approach to the problem and its what labs tend to do.  They would have a standard dataset with Gender, Age, Lower, Upper Extended Lower, Extended Upper.  This dataset is then merged onto the lab data by age and gender to get the normal ranges which are then used to calculate out of range flags.  

Look at the CDISC SDTM guidelines, its all there and has been thought out by many in the industry for quite some time.

Contributor
Posts: 22

Re: Syntax Help

the data on the blood count I took from a blood machine in Italy, and the parameters are those I'm creating a model to make the logic, however I did not understand how to create the new RBC dichocotoma variable for sex.
Super User
Posts: 9,611

Re: Syntax Help

[ Edited ]
Posted in reply to michshoot

For formatting code like that, you should be chased through SASVille with a priestess ringing a bell and repeatedly shouting "Shame! Shame! Shame!" following you.

 

And you unnecessarily complicate your conditions; try this instead:

data datasetdummy;
set WORK.PPP;
if Sesso = 'F'
then Sesso = 0;
else Sesso = 1; * nuova variabile binaria riguardante il sesso l'ho chiamata sesso (0=donna,1=uomo);
if Interno = 'SI'
then Interno = 0;
else Interno = 1; * interno (si=0,no=1);
if NEUT <= 90 or NEUT >= 450
then NEUT = 0;
else NEUT = 1; * se NEUT non si trova tra 90-450 è uguale a 0 altrimenti è uguale a 1;
if LYMPH <= 100 or LYMPH >= 300
then LYMPH = 0;
else LYMPH = 1; * uguale al commento sopra per tutte le variabili MONO EO BASO;
if MONO < 0 or MONO >= 100
then MONO = 0;
else MONO = 1;
if EO < 0 or EO >= 40
then EO = 0;
else EO = 1;
if BASO < 0 BASO >= 5
then BASO = 0;
else BASO = 1;
if PLT <= 130 or PLT >= 330
then PLT = 0;
else PLT = 1;
if MCV <= 800 or MCV >= 970
then MCV = 0;
else MCV = 1;
if MCH <= 270 or MCH >= 340
then MCH = 0;
else MCH = 1;
if MCHC <= 330 or MCHC >= 364
then MCHC = 0;
else MCHC = 1;
if WBC <= 2600 or WBC >= 7800
then WBC = 0;
else WBC = 1;
run;

Edit: changed MONO, EO and BASO.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 22

Re: Syntax Help

Posted in reply to KurtBremser
you did not answer my question?
how to implement eg 2 !!!
RBC?
that however is sas fault there are few online guides !!
Super User
Posts: 9,611

Re: Syntax Help

Posted in reply to michshoot

RBC.Sesso is not a valid SAS name for a variable. Look into the "Columns" section of the dataset properties window or into the output of proc contents to get the real name.

If that column really has that name because it was imported that way, use 'RBC.Sesso'n as the name in your code.

 

To find documentation, do a google search for "sas XXXX", where XXXX is the thing you want to know about (statement, procedure, error text, whatever). Works fine for me any time.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 22

Re: Syntax Help

Posted in reply to KurtBremser
no, they are two different variables. sesso is the sex (0=f,1=male) I have to create a new RCB variable with the values I have stratified by sex.
wbc for women (sex = 0) its range is from 370 to 500 the values out of the range become 0 inside equal to 1, for males (sex = 1) their range goes from 410 to 540 values out of the range become o and inside 1.
should the output be 0 and 1 in a single variable?
Super User
Posts: 9,611

Re: Syntax Help

[ Edited ]
Posted in reply to michshoot

michshoot wrote:
no, they are two different variables. sesso is the sex (0=f,1=male) I have to create a new RCB variable with the values I have stratified by sex.
wbc for women (sex = 0) its range is from 370 to 500 the values out of the range become 0 inside equal to 1, for males (sex = 1) their range goes from 410 to 540 values out of the range become o and inside 1.
should the output be 0 and 1 in a single variable?

So you will need to declare an array:

array rbc_sex {2} rbc_f rbc_m; /* or whatever your rbc for female/male are called */
if rbc_sex{sesso+1} <= 2600 or rbc_sex{sesso+1} >= 7800
then rbc = 0;
else rbc = 1;

Is that what you intend to do?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 22

Re: Syntax Help

Posted in reply to KurtBremser
data dsd;
set datasetdummy ;
if RBC=Sesso=0<=370 then RBC=0; else if RBC=Sesso=0>=500 ;else RBC=Sesso=0=1;
if RBC=Sesso=1<=410 then RBC=0; else if RBC=Sesso=1>=540 ;else RBC=Sesso=1=1;
run;
I was wrong to post it, I can say how to implement it I have 2 range permaschio and female, thanks ..
Super User
Posts: 9,611

Re: Syntax Help

Posted in reply to michshoot

Could you give an example for datasetdummy, so we can see the real names of your variables?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 22

Re: Syntax Help

Posted in reply to KurtBremser
Super User
Posts: 9,611

Re: Syntax Help

[ Edited ]
Posted in reply to michshoot

There's only one RBC and one WBC variable, nothing that is sex-specific. I still have no clue what you try to do with the syntax

if RBC=Sesso=0<=370

as that condition would only be true if RBC and Sesso were both 0. And the last part of the condition doesn't make any sense at all, as zero is always smaller than 370.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 22

Re: Syntax Help

Posted in reply to KurtBremser

by sesso = 0 is the woman by sesso= 1 is the man

they have different ranges
Solution
‎01-04-2018 11:28 AM
Super User
Posts: 13,084

Re: Syntax Help

Posted in reply to michshoot

michshoot wrote:

Hello to all,

 

how do I do if I have a sex-dependent variable?
wbc for women (sex = 0) its range is from 370 to 500 the values out of the range become 0 inside equal to 1, for males (sex = 1) their range goes from 410 to 540 values out of the range become o and inside 1.

should the output be 0 and 1 in a single variable?

I tried in the second part of the eg.

 


1. When recoding I generally place the values into a new variable so that errors of coding are easy to debug.

2. Often with ranges such as this I create custom formats as then I do not need to modify the data at all and most analysis procedures will use the formatted values as categories UNLESS I have an instance such as you have where the ranges differ by another characteristic such as gender or age.

 

You can use logic results to assign 0, 1 such as

 

   if sex=0 then do;
      wbc = (370 le wbc le 500);
   end;
   else do;
      wbc = (410 le wbc le 540);
   end;

SAS will assign a value of 1 to "true"

 

Contributor
Posts: 22

Re: Syntax Help

thank's
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 503 views
  • 1 like
  • 5 in conversation