BookmarkSubscribeRSS Feed
Tom
Super User Tom
Super User

Since GENDER_SP is length $2 it might be that the single digit codes are in the second character instead of the first. Or that the second character has some other non-graphic character other than a space.

Since it looks like the test you are making are non-overlapping it might make sense to do them as a series of ELSE clauses. Then you can include a catch all for any cases that do not fit.

 

DATA NovDec19.MERGED_NUMPARTS_1;
  SET NovDec19.MERGED_NovDec19_NUMPARTS;
  length mensex $3;
  if gender_sp ="1" AND NUM_PRT_3M="1" THEN MENSEX= "1";
  else if left(gender_sp) ="1" AND NUM_PRT_3M="2" THEN MENSEX="2";
  else if left(gender_sp)="1" AND NUM_PRT_3M="3" THEN MENSEX="3";
  else if left(gender_sp)="1" AND NUM_PRT_3M="5" THEN MENSEX="5";
  else if left(gender_sp) in ("3" , "9") AND NUM_PRT_3M in ("1" , "2" , "3" , "5") THEN MENSEX="999";
  else MENSEX='XXX';
run;

And most import make sure you are checking the NEW dataset and not the old one when check whether the re-code worked.

 

Dhana18
Obsidian | Level 7
Alphabetic List of Variables and Attributes

Variable

Type

Len

Format

Informat

Label

NUM_PRT_3M

Char

1

gender_sp

Char

2

gender_sp


PaigeMiller
Diamond | Level 26

As requested earlier by @ballardw , you need to provide us with a portion of this data set, using the instructions in the link he provided. We do not want screen captures, we do not want Excel files or attachments, we need to see a portion of this data in that format and not any other format.

--
Paige Miller
ballardw
Super User

@Dhana18 wrote:
gender_sp NUM_PRT_3M mensex Frequency
1 0 999 128
1 1 999 128
1 2 999 70
1 3 999 8
1 5 999 16
2 0 999 193
2 1 999 144
2 2 999 107
2 3 999 57
2 5 999 40
3 0 999 17
3 1 999 4
3 2 999 13
3 3 999 11
3 5 999 6
9 0 999 44
9 1 999 3
9 2 999 3
9 5 999 3

One thing that we learn eventually about SAS is that it tries to help us make "nice" output. Sometimes that interferes with diagnosing problems when using output from procedures to examine values. One of things that happens is that most procedures will strip leading spaces from values. So it is possible that the 1, 2, 3 and 9 above are actually " 1", " 2", " 3" and " 9" for the gender_sp variable so comparisons of equality such as ="1" fail.

 

Here is a brief example you can run. Note that the row heading for Y in the second table now is indented because the display Style instruction says to use the leading space:

data example;
   x='2';
   y=' 2';
run;

proc tabulate data=example;
title "Default display removes leading blanks";
   class x y;
   table x y , n;

run;title;
proc tabulate data=example;
title "ASIS option shows leading blanks";
   class x y;
   classlev y / style=[Asis=on];
   table x y , n;

run;title;

Which is one of the reasons that we ask about actual data an not proc output.

ballardw
Super User

@Dhana18 wrote:
gender_sp NUM_PRT_3M mensex Frequency
1 0 999 128
1 1 999 128
1 2 999 70
1 3 999 8
1 5 999 16
2 0 999 193
2 1 999 144
2 2 999 107
2 3 999 57
2 5 999 40
3 0 999 17
3 1 999 4
3 2 999 13
3 3 999 11
3 5 999 6
9 0 999 44
9 1 999 3
9 2 999 3
9 5 999 3

You are showing values of gender_sp and Num_prt_3m not in the code with results.

So I have to assume that you have some other code affecting your MENSEX variable.

 

Here is an example data set that just uses character values of those shown in your freq output above:

data example;
   length gender_sp $ 1 Num_prt_3m $ 2 mensex $ 3;
   do gender_sp='1','2','3','9';
      do Num_prt_3m = '0','1','2','3','5';
         if gender_sp ="1" AND NUM_PRT_3M="1" THEN MENSEX="1";
         if gender_sp ="1" AND NUM_PRT_3M="2" THEN MENSEX="2";
         if gender_sp ="1" AND NUM_PRT_3M="3" THEN MENSEX="3";
         if gender_sp ="1" AND NUM_PRT_3M="5" THEN MENSEX="5";
         if gender_sp in("3" , "9") AND NUM_PRT_3M in("1" "2" "3" "5") THEN MENSEX="999";
         output;
         /* reset result before next loop iteration*/
         call missing(mensex);
      end;
   end;
run;

Note that we get every possible combination of the one character length values of the two input variables.

And the result of:

proc freq data=example;
  tables gender_sp*Num_prt_3m*mensex/missing list nopercent nocum;
run;

is

The FREQ Procedure
gender_sp Num_prt_3m mensex Frequency
1 0   1
1 1 1 1
1 2 2 1
1 3 3 1
1 5 5 1
2 0   1
2 1   1
2 2   1
2 3   1
2 5   1
3 0   1
3 1 999 1
3 2 999 1
3 3 999 1
3 5 999 1
9 0   1
9 1 999 1
9 2 999 1
9 3 999 1
9 5 999 1

 

Which does not have some of the output values you show for the same input. So either 1) your input is different than implied by the Proc Freq output and/or 2) your code as executed includes something you have not shown.

Such as a value set in a previous data step, read from the source file, or manipulated after assignment in this data step.

I suggest that you carefully search your code for every instance of MENSEX and see if you have another assignment or possibly and array using that variable.

 

I had removed the commas in the last comparison just incase your pasted code had contained a non-visible character that was causing problems.

 

 

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