BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
darkrainbow
Calcite | Level 5
I have two variables for employment status: one for parent 1 and another for parent 2. There are 3 values for both of them.
1-employed
2-unemployed
and L-logical skip if parent is not alive.

I would like to create a new variable out of both variables using the logical step value.

This is my code:
data version2017;
set version2017;
if Parent1=1 AND parent2=1 then employment=1; *Both parents employed;
else if (parent1=1 AND parent2 IN (2,.)) OR (parent1 IN (2, .) AND parent2=1) then employment=2; *at least one parent employed;
else if parent1=2 AND parent2=2 then employment=3; *no adult employed;
else employment =.;
run;

Unfortunately, my code doesn’t work. I am unable to treat L as a character because my variable is numeric. How can I factor L into my code?
1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

If your variable Parent1 is numeric (run PROC CONTENTS to check the type) and you see values that display as "L" in PROC PRINT or PROC FREQ, then the value is a numeric SPECIAL MISSING VALUE.    See e.g.: https://documentation.sas.com/doc/en/lrcon/9.4/p1xr9fm7y8kek5n1hpj008tnu1a1.htm or the first SAS paper I wrote, 23 years ago: https://www.lexjansen.com/nesug/nesug01/ps/ps8009.pdf  .

 

You can refer to the value in code like:

 

if Parent1=.L AND parent2=.L then employment=.L; *neither parent is alive;

 

 

 

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.

View solution in original post

4 REPLIES 4
Quentin
Super User

If your variable Parent1 is numeric (run PROC CONTENTS to check the type) and you see values that display as "L" in PROC PRINT or PROC FREQ, then the value is a numeric SPECIAL MISSING VALUE.    See e.g.: https://documentation.sas.com/doc/en/lrcon/9.4/p1xr9fm7y8kek5n1hpj008tnu1a1.htm or the first SAS paper I wrote, 23 years ago: https://www.lexjansen.com/nesug/nesug01/ps/ps8009.pdf  .

 

You can refer to the value in code like:

 

if Parent1=.L AND parent2=.L then employment=.L; *neither parent is alive;

 

 

 

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.
darkrainbow
Calcite | Level 5

This worked. Thank you!

ballardw
Super User

First you may want to share what you think you want to do with the  "skip" variable.

I'm not seeing anything obvious for a variable named Employment what that would contribute to.

 

Second, do you see an L in the data set for either of those variables? If the variable is numeric and you see L then that means the variable has been assigned a special missing, which is a dot plus a letter or the underscore character. In which case you can test for the special value by using .L

These special missing values will appear in table views or printed output as upper case letters even when assigned with lower case.

How to use:

If variable = .L then do <whatever you were going to do>.

An example creating a special missing and printing:

data junk;
   x=.a;
run;

proc print data=junk;
run;

One reason for special missing values is that you can test for the condition(s) and use differently than a basic missing. Or create a custom format to display different text such as "Skipped" or "Not Answered" or "Refused" so you have more information about why at least some of the values are missing.

 

 

If a variable essentially has two values it is often quite advantageous to code the values as 1/ 0 where the 1 goes to the level most often of interest.

Reason is many tasks become much simpler. If the values were 1/0 with 1=Employed and 0=unemployed then

employment= sum(Parent1, Parent2);

would mean: employment=2 both employed, 1= only one employed, 0=no adult employed. You would get a missing value only if both of the variables were missing. Reason to use sum function is Parent1+Partent2 would return a missing value if either of the variables were missing.

Tom
Super User Tom
Super User

Unless you have some user defined format attached to the variable then numbers that PRINT as the single letter uppercase L contain the special missing value of .L .

 

So you can test if the value is .L exactly using one of these ways:

parent1 = .L
parent1 = .l
parent1 in (.L)
parent1 in (.l)

Or you can test if it has any of the 28 different missing values that SAS supports by using the MISSING function.

missing(parent1)

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1847 views
  • 2 likes
  • 4 in conversation