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

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.

 

Can someone help please?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Look at this example for your logic:

select (professionalcontact);
  when ('') visit = .;
  when ('Admin', 'unkno') delete;
  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.

View solution in original post

10 REPLIES 10
sks521
Quartz | Level 8

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;

Cynthia_sas
SAS Super FREQ

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

sks521
Quartz | Level 8

Hi Thanks, here is the freq proc;

 

The FREQ Procedure
ProfessionalContact Frequency Percent CumulativeFrequency CumulativePercentAHPAdminGPHCANurseUnknoFrequency Missing = 1
6603.586603.58
692337.54758341.12
814744.181573085.29
680.371579885.66
192510.441772396.10
7193.9018442100.00
sks521
Quartz | Level 8

Sorry it changed the format a little bit;

 

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

 

AHP

Admin

GP

HCA

Nurse

Unkno

sks521
Quartz | Level 8

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

 

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

 

Kurt_Bremser
Super User

@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).

sks521
Quartz | Level 8

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

Kurt_Bremser
Super User

Look at this example for your logic:

select (professionalcontact);
  when ('') visit = .;
  when ('Admin', 'unkno') delete;
  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.

sks521
Quartz | Level 8

Very helpful indeed! thanks for all your help

 

S

tsap
Pyrite | Level 9

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

 

Use the instructions on this page to help, https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... .

 

 

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 10 replies
  • 999 views
  • 2 likes
  • 4 in conversation