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

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
Frequency

Cumulative
Percent

.

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
Frequency

Cumulative
Percent

.

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
urban58
Quartz | Level 8
It is much simpler - thank you and it works beautifully with my actual dataset.
Much appreciated!
Margaret
PaigeMiller
Diamond | Level 26

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
Tom
Super User Tom
Super User

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;

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!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 543 views
  • 0 likes
  • 3 in conversation