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
S
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.
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;
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
Hi Thanks, here is the freq proc;
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.10 |
719 | 3.90 | 18442 | 100.00 |
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
And here is snapshot of the the new variable 'visit' created from existing variable 'professionalcontact'
. | . | |
1 | GP | . |
1 | Nurse | . |
1 | Nurse | . |
1 | Admin | . |
1 | GP | . |
1 | Admin | . |
1 | Admin | . |
1 | GP | . |
1 | GP | . |
@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).
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
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.
Very helpful indeed! thanks for all your help
S
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
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.