- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a SAS dataset with a non formatted "visit" variable that I want to make numeric but keep the character information
proc format;
value $visit
1 ="1:offsite"
2 ="2:hospital"
3 ="3:remote"
.='.';
run;
data have;
input id 1 visit $ 2-10;
datalines;
1 hospital
2 offsite
3 hospital
4 remote
5
6 hospital
7 remote
8 hospital
;
run;
data want;
set have;
if visit='offsite' then visit=1; else
if visit='hospital' then visit=2; else
if visit='remote' then visit=3; else
visit=.;
format visit $visit.;
run;
proc freq data=want;
tables visit / list ;
run;
output
visit |
Frequency |
Percent |
Cumulative |
Cumulative |
. |
1 |
12.50 |
1 |
12.50 |
1 |
1 |
12.50 |
2 |
25.00 |
2 |
4 |
50.00 |
6 |
75.00 |
3 |
2 |
25.00 |
8 |
100.00 |
Instead I would like to see
visit |
Frequency |
Percent |
Cumulative |
Cumulative |
. |
1 |
12.50 |
1 |
12.50 |
1:offsite |
1 |
12.50 |
2 |
25.00 |
2:hospital |
4 |
50.00 |
6 |
75.00 |
3:remote |
2 |
25.00 |
8 |
100.00 |
I will get the output I want if I do this and remove proc format - is this the only way I can do it?
I would like to have visit as a numeric variable
data want;length visit $10;
set have;
if visit='offsite' then visit="1:offsite"; else
if visit='hospital' then visit="2:hospital"; else
if visit='remote' then visit="3:remote"; else
visit=.;
format visit $visit.;
run;
Would appreciate any help.
Margaret
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can't have visit as numeric, because visit is created as character.
It's also not clear why you are jumping through hoops to change variables from numeric to character and format them etc.
This seems a lot simpler
proc format;
value $visit
'offsite' ="1:offsite"
'hospital' ="2:hospital"
'remote' ="3:remote"
' '='.';
run;
data have;
input id 1 visit $ 2-10;
datalines;
1 hospital
2 offsite
3 hospital
4 remote
5
6 hospital
7 remote
8 hospital
;
run;
proc freq data=have order=formatted;
tables visit / list missing;
format visit $visit.;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can't have visit as numeric, because visit is created as character.
It's also not clear why you are jumping through hoops to change variables from numeric to character and format them etc.
This seems a lot simpler
proc format;
value $visit
'offsite' ="1:offsite"
'hospital' ="2:hospital"
'remote' ="3:remote"
' '='.';
run;
data have;
input id 1 visit $ 2-10;
datalines;
1 hospital
2 offsite
3 hospital
4 remote
5
6 hospital
7 remote
8 hospital
;
run;
proc freq data=have order=formatted;
tables visit / list missing;
format visit $visit.;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Much appreciated!
Margaret
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Of course, the answer is even simpler is you don't insist that you have offsite first and ordering isn't an issue.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It looks like your last step is trying to recode VIST from so that should work without any formats.
And since you have recoded VISIT then you do NOT want to use the new values with the old format. To remove any format that might be attached to VISIT you use a FORMAT statement that lists variable(s) but no format.
data want;
length visit $10;
set have;
if visit='offsite' then visit="1:offsite";
else if visit='hospital' then visit="2:hospital";
else if visit='remote' then visit="3:remote";
else visit=.;
format visit ;
run;