Help using Base SAS procedures

IF/THEN Statement not Recognizing specific levels in Categorical Variable

Accepted Solution Solved
Reply
Contributor
Posts: 69
Accepted Solution

IF/THEN Statement not Recognizing specific levels in Categorical Variable

Hello,

I am trying to recode a 5 level categorical variable into a dichotomous variable and SAS  is not recognizing the last three levels: "Dont Know/ Unsure", 'Parents not married' , and 'Refused'.

data probably;

set temp3;

IF ACEDIVRC  = 1              THEN test  = 1;

IF ACEDIVRC  = 'No' OR ACEDIVRC  = 'Dont Know/ Unsure' OR ACEDIVRC  = 'Parents not married' OR ACEDIVRC  = 'Refused' THEN test  = 2;

IF ACEDIVRC  = ' '  THEN test  = '.';

Run;

When I run a proc freq it only shows the frequencies "No" level in the second level. I wrote another code to just look at one of the other levels and chose the "Refused" level because it doesn't have any spaces in its name. This is the Code:

data probably;

set temp3;

IF ACEDIVRC  = 1 THEN test1    = 1;

IF ACEDIVRC  = 2 THEN test2    = 2;

IF ACEDIVRC  = 'Refused' THEN test3  = 3;

Run;

proc freq data = probably;

tables ACEDIVRC  test1 test2 test3 /MISSING;

run;

The proc freq automatically defaults the third level to the frequency of the missing values (See below, test3). Any idea how to fix this issue?

   ACEDIVRC    
.86268.3686268.36
Yes1829217.722691826.08
No7495772.6310187598.71
Dont Know/ Unsure3050.3010218099.01
Parents not married5190.5010269999.51
Refused5040.49103203100.00

   test1    
.8491182.288491182.28
11829217.72103203100.00

   test2    
.2824627.372824627.37
27495772.63103203100.00

   test3    
.9457791.649457791.64
386268.36103203100.00

Thank you!

I am using SAS 9.4.


Accepted Solutions
Solution
‎07-28-2015 10:57 AM
Regular Contributor
Posts: 156

Re: IF/THEN Statement not Recognizing specific levels in Categorical Variable

Could you give us a proc contents listing of "PROBABLY".  specifically what is the variable type of ACEDIVRC    and does it have a format assigned to it.

View solution in original post


All Replies
Solution
‎07-28-2015 10:57 AM
Regular Contributor
Posts: 156

Re: IF/THEN Statement not Recognizing specific levels in Categorical Variable

Could you give us a proc contents listing of "PROBABLY".  specifically what is the variable type of ACEDIVRC    and does it have a format assigned to it.

Contributor
Posts: 69

Re: IF/THEN Statement not Recognizing specific levels in Categorical Variable

Ah PhilC! I think you just solved my problem! I combined 20 state-level BRFSS datasets with modular data and one of the states had formats for specific variables, including ACEDIVRC. If I remove the format, it should work fine.

Thank you!

Regular Contributor
Posts: 156

Re: IF/THEN Statement not Recognizing specific levels in Categorical Variable

Glad to help, you don't need to remove the format, but when you run PROC FREQ you can issue a FORMAT statement so that you may see the unformatted values. 

proc freq data = probably;
  FORMAT ACEDIVRC  ;
  tables ACEDIVRC  test1 test2 test3 /MISSING;
run;

Super User
Posts: 17,815

Re: IF/THEN Statement not Recognizing specific levels in Categorical Variable

For text comparisons the words are case sensitive.

  1. Instead of multiple OR use IN
  2. Upcase all words to make sure there is no case sensitivity issues
  3. You're mixing character and numeric types. If Acedivrc is character you need to quote all values, 1 vs "1"
  4. Trim words to make sure no trailing spaces cause issues
  5. Use missing function to find missing values
  6. Missing is . in numeric, not a character so no quotes
  7. Use Else IF to allow faster processing
  8. Add a final ELSE to catch any missed conditions.

data probably;

set temp3;

   IF ACEDIVRC  = '1'  THEN test  = 1;

      ELSE IF upper(trim(ACEDIVRC))  in ('NO' 'DONT KNOW/ UNSURE' 'PARENTS NOT MARRIED' 'REFUSED') THEN test  = 2;

       else IF missing(ACEDIVRC)  THEN test  = .;

       else test = 99;

Run;

Contributor
Posts: 74

Re: IF/THEN Statement not Recognizing specific levels in Categorical Variable

You're mixing up data types in both ACEDIVRC and TEST.  Be sure to know what is numeric and what is character.  Also the IN operator would make this a bit easier to read:

data temp3;

    length acedivrc $20;

    acedivrc = '1'; output;

    acedivrc = 'No'; output;

    acedivrc = "Don't know/Unsure"; output;

    acedivrc = 'Parents not married'; output;

    acedivrc = 'Refused'; output;

run;

data probably;

    set temp3;

    if acedivrc = '1' then test = 1;

    else if acedivrc in ('No',"Don't know/Unsure",'Parents not married', 'Refused') then test = 2;

    else if missing(acedivrc) then test = .;

run;

Hope this helps.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 436 views
  • 1 like
  • 4 in conversation