BookmarkSubscribeRSS Feed
SASstudent2013
Calcite | Level 5

Hi. I'm trying to create a new variable. I have variable 'labor' where less than 37 weeks is premature, and anything 37 and above is normal. I want to create a new variable called 'delivery' where 1=premature and 2=normal, then figure out how many premature and normal deliveries there are. Below is the code I'm using:

data hosp_data;

if labor < 37 then delivery=1;

else if labor >= 37 then delivery=2;

run;

And here's what the log tells me:

NOTE: Variable labor is uninitialized.

NOTE: The data set HOSP_DATA has 1 observations and 2 variables.

The output shows 1 missing observation.

What am I doing wrong??

20 REPLIES 20
AncaTilea
Pyrite | Level 9

Hi.

First, you need to SET your data set...like,

data hosp_data;

     set data_you_have;


Then, I would (just out of habit) make sure to exclude the missing values when you create delivery = 1:

if . < labor < 37 then delivery=1;

else if labor >= 37 then delivery=2;

Also, it would be 'cleaner' to just create an indicator for delivery premature yes/no:

proc format;

     value yesno 1 = "Premaure" 0 = "Not premature";quit;

data hosp_data;

     set data_you_have;

delivery_premature = (. < labor < 37);

     format delivery_premature yesno.;

run;

SASstudent2013
Calcite | Level 5

Thanks. When I try it the first way, I get an error message saying the set statement doesn't exist...

Code:

data hosp_data;

     set data_case2;

if . < labor < 37 then delivery=1;

else if labor >= 37 then delivery=2;

run;

LOG ERROR: ERROR: File WORK.DATA_CASE2.DATA does not exist.

Do you know why I'm getting that error?

AncaTilea
Pyrite | Level 9

Do you have the data set named:l data_case2 in your WORK library, or is it a permanent file somewhere else??

SASstudent2013
Calcite | Level 5

This is how I start my code (and I get an error with the libname statement):

libname data1;

proc import datafile="...hosp.xls"

out=hosp_data1;

ERROR: data1 is not a valid SAS name.

So not sure if that will help?

Reeza
Super User

So you have errors from the beginning? Then your errors will carry through. You may want to review this website

SAS Class Notes to learn more about SAS.

Additionally, start debugging your code line by line or proc by proc from the top.

AncaTilea
Pyrite | Level 9

so, libname data1.... is missing a path

the reason you create a libname is to either retrieve or store a permanent  data set.

libname data1 "give a path name such as C:\Documents and Settings\User";

proc import datafile="...hosp.xls"

out=hosp_data1;

Ok, give us more code so we can figure out where things break....

robertrao
Quartz | Level 8

Hi,

data hosp_data;

set data_you_have;

delivery_premature = (. < labor < 37);    /*what does this step do??creating a new variable called delivery_premature and what does (. < labor < 37) do;

format delivery_premature yesno.;

run;


AncaTilea
Pyrite | Level 9

Hi.

This line

delivery_premature = (. < labor < 37);  

is similar to

if (. < delivery < 37) then delivery_premature = 1;

     else delivery_premature = 2;

IT is basically a short way to create a dummy variable (0/1).for a given condition.

robertrao
Quartz | Level 8

Could you please explain with a small example??

Regards

robertrao
Quartz | Level 8

Hi.

This line

delivery_premature = (. < labor < 37);  

is similar to

if (. < delivery < 37) then delivery_premature = 1;

     else delivery_premature = 2;                               /*should have been 0 instead of 2 here????????*/

IT is basically a short way to create a dummy variable (0/1).for a given condition.

robertrao
Quartz | Level 8

Hi I tried this example and it does not work..Could you please explain

data have;
input labor;
cards;
35
43
30
33
22
20
15
19
37
36
38
39
;
run;

data have1;
set have;
if . < labor < 37 then delivery=1;

else if labor >= 37 then delivery=2;
run;

proc format;
value yesno 1 = "Premaure"
0 = "Not premature";
quit;

data have2;
set have1;
if (. < delivery < 37) then delivery_premature = 1;

     else delivery_premature = 2;
format delivery_premature yesno.;
run;

Reeza
Super User

You set delivery to only 1 or 2 in the first step (have1) but then are checking if its between . and 37 in the second step. delivery_premature will always be 1 won't it?

I think your looking for the following.

data have;
input labor;
cards;
35
43
30
33
22
20
15
19
37
36
38
39
;
run;

proc format;

value yesno 1 = "Premature"

0 = "Not premature";

quit;

data have1;
set have;
if . < labor < 37 then delivery=1;

else if labor >= 37 then delivery=2;

format delivery yesno.;
run;

robertrao
Quartz | Level 8

Hi, i understood it better now

and since the format has 0 and 1 only. So should it not be:

data have1;
set have;
if . < labor < 37 then delivery=1;

else if labor >= 37 then delivery=2;  /*should it not be 0 here*/

format delivery yesno.;
run;

AncaTilea
Pyrite | Level 9

I don't understand the confusion, Robert.

You want to be able to tell when a delivery is strictly less than 37 months, and when it is greater than 37 (inclusive) months.

So, based on the data set have that you used in the above reply, you get exactly that when you run this code:

proc format;

value yesno 1 = "Premature"

0 = "Not premature";

quit;

data have2;

set have;

delivery_premature = (. < labor < 37);  

format delivery_premature yesno.;

run;

Please let me know what happens when you run this above code??

when you use delivery_premature = (. < delivery < 37) ....what this translates to is:

SAS please put a 1 whenever this condition (. < delivery < 37) is true, and put a 0 otherwise, which should save you from writing the next line (else if delivery >= 37)

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 20 replies
  • 1252 views
  • 0 likes
  • 4 in conversation