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

I'm trying to create a variable with 3 levels (0, 1, and 2) just using a simple if/then statement from other binary variables. However, the 0 level is not showing up when I then run a proc freq/tables. I can't seem to figure out what's going wrong. Is the 0 level being overwritten because of how I've coded it? Or something else I'm not seeing? Here's my code: 

 

data pk06;
set pk06;
if bcg=0 and polio1=0 and polio0=0 and measles=0 and dtp1=0 then fullvax=0;
else if bcg=1 and polio_full=1 and measles=1 and dtp3=1 then fullvax=2;
else fullvax=1;
run;

proc freq data=pk06;
where 1 le b8 le 4;
tables fullvax;
run;

And here's a screenshot of the table I'm getting:

 

joachimg_0-1638399005619.png

 

I'm fairly confident that there should be observations that fall into the 0 level, and I would think that even if there were 0 observations there, I would get a row that had 0 as the frequency. Let me know if you need more information! Thanks so much. 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Honestly we can't tell. You're using different variables in your conditions, and we don't know your variables or how they relate so there's not an easy way to figure this out.

Try filtering and see if you get any records? If this doesn't return records it means your condition isn't right somehow.

data check;
set pk06;
where bcg=0 and polio1=0 and polio0=0 and measles=0 and dtp1=0 and 1 le b8 le 4;
run;

 


@joachimg wrote:

I'm trying to create a variable with 3 levels (0, 1, and 2) just using a simple if/then statement from other binary variables. However, the 0 level is not showing up when I then run a proc freq/tables. I can't seem to figure out what's going wrong. Is the 0 level being overwritten because of how I've coded it? Or something else I'm not seeing? Here's my code: 

 

data pk06;
set pk06;
if bcg=0 and polio1=0 and polio0=0 and measles=0 and dtp1=0 then fullvax=0;
else if bcg=1 and polio_full=1 and measles=1 and dtp3=1 then fullvax=2;
else fullvax=1;
run;

proc freq data=pk06;
where 1 le b8 le 4;
tables fullvax;
run;

And here's a screenshot of the table I'm getting:

 

joachimg_0-1638399005619.png

 

I'm fairly confident that there should be observations that fall into the 0 level, and I would think that even if there were 0 observations there, I would get a row that had 0 as the frequency. Let me know if you need more information! Thanks so much. 


 

View solution in original post

4 REPLIES 4
Reeza
Super User

Honestly we can't tell. You're using different variables in your conditions, and we don't know your variables or how they relate so there's not an easy way to figure this out.

Try filtering and see if you get any records? If this doesn't return records it means your condition isn't right somehow.

data check;
set pk06;
where bcg=0 and polio1=0 and polio0=0 and measles=0 and dtp1=0 and 1 le b8 le 4;
run;

 


@joachimg wrote:

I'm trying to create a variable with 3 levels (0, 1, and 2) just using a simple if/then statement from other binary variables. However, the 0 level is not showing up when I then run a proc freq/tables. I can't seem to figure out what's going wrong. Is the 0 level being overwritten because of how I've coded it? Or something else I'm not seeing? Here's my code: 

 

data pk06;
set pk06;
if bcg=0 and polio1=0 and polio0=0 and measles=0 and dtp1=0 then fullvax=0;
else if bcg=1 and polio_full=1 and measles=1 and dtp3=1 then fullvax=2;
else fullvax=1;
run;

proc freq data=pk06;
where 1 le b8 le 4;
tables fullvax;
run;

And here's a screenshot of the table I'm getting:

 

joachimg_0-1638399005619.png

 

I'm fairly confident that there should be observations that fall into the 0 level, and I would think that even if there were 0 observations there, I would get a row that had 0 as the frequency. Let me know if you need more information! Thanks so much. 


 

joachimg
Obsidian | Level 7
Thank you! I haven't fully figured out what's going on yet, but this was really helpful in getting me to the next step. 🙂
ballardw
Super User

First warning: Use of the code construct with the input and output data set the same name is a likely problem because you may have left over erroneous values from a previous run. When recoding to create new variables strongly recommend creating a new data set so you can compare before/after results without the possibility of overwriting any of your existing data values.

data pk06;
set pk06;

 Code to check recoding behavior would look like like:

proc freq data=pk06;
tables bcg* polio1* polio0* polio_full measles* dtp1* dpt3* fullvax/ list missing;
run;

The LIST option makes the values all appear on one row so you can see if the values are recoding correctly for each combination of the variables. The MISSING brings in values that you have not really addressed (appearing in the Else). You do not check results with with a filter like the Where statement unless that is really a requirement when assigning the values. Your where may have removed all the 0 Fullvax values for all we can see.

 

 

ChrisNZ
Tourmaline | Level 20

You simply don't have data that matches:

bcg=0 and polio1=0 and polio0=0 and measles=0 and dtp1=0 and 1 le b8 le 4

 Your test syntax is fine and nothing gets overwritten. 

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!

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
  • 4 replies
  • 517 views
  • 2 likes
  • 4 in conversation