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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PhilC
Rhodochrosite | Level 12

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

5 REPLIES 5
PhilC
Rhodochrosite | Level 12

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.

daszlosek
Quartz | Level 8

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!

PhilC
Rhodochrosite | Level 12

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;

Reeza
Super User

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;

evp000
Quartz | Level 8

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.

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
  • 5 replies
  • 2338 views
  • 1 like
  • 4 in conversation