Quartz | Level 8

## Creating new variable

I want to create a new variable from an existing variable.

The existing variable has following values- GP, AHP, HCA, Nurse, admin

I want to create a new variable which does not include 'admin' and missing values, and coded '1' for all other values (GP, AHP, HCA, Nurse).

I have written the following syntax but it's giving '.' for all values in the new variable.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Creating new variable

Look at this example for your logic:

``````select (professionalcontact);
when ('') visit = .;
when ('GP', 'AHP', 'HCA', 'Nurse') visit = 1;
otherwise; /* if you omit this, you will be alerted to an unexpected value by an ERROR */
end;``````

I just noted that you actually want to have a numeric value in visit, so ignore that part of my previous post.

10 REPLIES 10
Quartz | Level 8

## Re: Creating new variable

Sorry forgot to post the syntax;

if professionalcontact=. then visit=.;
else if professionalcontact in ('Admin', 'unkno') then delete;*this gives 11520 observations/visits;
else if professionalcontact in ('GP', 'AHP', 'HCA', 'Nurse') then visit=1;

SAS Super FREQ

## Re: Creating new variable

Hi:

The first thing I would do is run a PROC FREQ on the input data set to find out what values _EXACTLY_ are in the field or variable PROFESSIONALCONTACT -- to check case, length, etc. Then you would be able to tell whether the number deleted by your IF statement was correct.

Cynthia

Quartz | Level 8

## Re: Creating new variable

Hi Thanks, here is the freq proc;

The FREQ Procedure
ProfessionalContact Frequency Percent CumulativeFrequency CumulativePercentAHPAdminGPHCANurseUnknoFrequency Missing = 1
 660 3.58 660 3.58 6923 37.54 7583 41.12 8147 44.18 15730 85.29 68 0.37 15798 85.66 1925 10.44 17723 96.1 719 3.9 18442 100
Quartz | Level 8

## Re: Creating new variable

Sorry it changed the format a little bit;

Each value in the left column represent number for each of the following categories;

AHP

GP

HCA

Nurse

Unkno

Quartz | Level 8

## Re: Creating new variable

And here is snapshot of the the new variable 'visit' created from existing variable 'professionalcontact'

PKid      ProfessionalContact visit
 . . 1 GP . 1 Nurse . 1 Nurse . 1 Admin . 1 GP . 1 Admin . 1 Admin . 1 GP . 1 GP .

Super User

## Re: Creating new variable

@sks521 wrote:

Sorry forgot to post the syntax;

if professionalcontact=. then visit=.;
else if professionalcontact in ('Admin', 'unkno') then delete;*this gives 11520 observations/visits;
else if professionalcontact in ('GP', 'AHP', 'HCA', 'Nurse') then visit=1;

The dot represents missing numeric values, but you work with character variables. Use an empty string ("") to represent missing character values.

Since your first use of the new variable was numeric, it was defined as such, and all attempts to assign character values to it will result in missing values, and NOTEs with ERRORs and finally a WARNING.

Before you expand this if/then/else chain, take a look at the data step select statement (not the SQL one).

Quartz | Level 8

## Re: Creating new variable

Thanks for helping but I couldn't get 'Before you expand this if/then/else chain, take a look at the data step select statement (not the SQL one).'. Can you elaborate further please?

BW

S

Super User

## Re: Creating new variable

Look at this example for your logic:

``````select (professionalcontact);
when ('') visit = .;
when ('GP', 'AHP', 'HCA', 'Nurse') visit = 1;
otherwise; /* if you omit this, you will be alerted to an unexpected value by an ERROR */
end;``````

I just noted that you actually want to have a numeric value in visit, so ignore that part of my previous post.

Quartz | Level 8

S

Pyrite | Level 9

## Re: Creating new variable

If possible, can you provide the logic to create a dummy data set that aligns with the dataset you are working to manipulate?

That would give a better starting point for us to understand what the starting data looks like and how the logic you are using afterwards is interacting with it.

Thanks

Discussion stats
• 10 replies
• 542 views
• 2 likes
• 4 in conversation