Help using Base SAS procedures

Creating new variable but everything goes missing?

Reply
Contributor
Posts: 23

Creating new variable but everything goes missing?

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??

Super Contributor
Posts: 543

Re: Creating new variable but everything goes missing?

Posted in reply to SASstudent2013

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;

Contributor
Posts: 23

Re: Creating new variable but everything goes missing?

Posted in reply to AncaTilea

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?

Super Contributor
Posts: 543

Re: Creating new variable but everything goes missing?

Posted in reply to SASstudent2013

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

Contributor
Posts: 23

Re: Creating new variable but everything goes missing?

Posted in reply to AncaTilea

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?

Super User
Posts: 19,789

Re: Creating new variable but everything goes missing?

Posted in reply to SASstudent2013

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.

Super Contributor
Posts: 543

Re: Creating new variable but everything goes missing?

Posted in reply to SASstudent2013

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....

Super Contributor
Posts: 1,041

Re: Creating new variable but everything goes missing?

Posted in reply to AncaTilea

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;


Super Contributor
Posts: 543

Re: Creating new variable but everything goes missing?

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: Creating new variable but everything goes missing?

Posted in reply to AncaTilea

Could you please explain with a small example??

Regards

Super Contributor
Posts: 1,041

Re: Creating new variable but everything goes missing?

Posted in reply to AncaTilea

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.

Super Contributor
Posts: 1,041

Re: Creating new variable but everything goes missing?

Posted in reply to AncaTilea

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;

Super User
Posts: 19,789

Re: Creating new variable but everything goes missing?

Posted in reply to robertrao

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;

Super Contributor
Posts: 1,041

Re: Creating new variable but everything goes missing?

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;

Super Contributor
Posts: 543

Re: Creating new variable but everything goes missing?

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)

Ask a Question
Discussion stats
  • 20 replies
  • 395 views
  • 0 likes
  • 4 in conversation