BookmarkSubscribeRSS Feed
urban58
Quartz | Level 8

Hello SAS community,

I have variables with the following

original format

proc format;

value s
0 ="Never/almost Never"
1 ="Once in a while"
2 ="Some days"
3 ="Most days"
4 ="Everyday"
5 ="Many times/day"
. ="Unknown";

I'm now being asked to do reverse formatting for a bunch of variables

/*1 = many times a day, 2 = every day, 3 = most days, 4 = some days, 5 = once in a while, 6 = never*/

data have;

input id strngth peace beauty prscnc union love;

cards;

1  0 1 2 3 4 5

2  4 0 0 1 3 4

3  4 2 3 2 5 0

4  2 4 4 0 2 3

5  0 3 5 0 0 1

6  3 5 1 4 3 4

7  5 2 3 2 1 0

8  3 2 2 5 2 1

9  5 1 3 2 1 0

10 4 0 0 1 3 4

11 0 2 3 2 5 0

12 2 4 4 0 2 3

;

run;

 

data have2;

set have;

format strngth peace beauty prscnc union love s.;

run; 

 

proc freq data=have2;

title 'orig';

tables strngth / list missing;

run;

proc format;

value sr

6 ="Never/almost Never"

5 ="Once in a while"

4 ="Some days"

3 ="Most days"

2 ="Everyday"

1 ="Many times/day"

. ="Unknown";

run;

data want;

set have2;

if strngth = 5 then strngth2 = 1; else

if strngth = 4 then strngth2 = 2; else

if strngth = 3 then strngth2 = 3; else

if strngth = 2 then strngth2 = 4; else

if strngth = 1 then strngth2 = 5; else

if strngth = 0 then strngth2 = 6; else

strngth2 = .;

 

format strngth2  sr.;

run;

but I'm getting the following for recoded - unable to copy in both tables:

original

strngth

Frequency

Percent

Cumulative
Frequency

Cumulative
Percent

Never/almost Never

3

25.00

3

25.00

Some days

2

16.67

5

41.67

Most days

2

16.67

7

58.33

Everyday

3

25.00

10

83.33

Many times/day

2

16.67

12

100.00

recoded

strngth2

Frequency

Percent

Cumulative
Frequency

Cumulative
Percent

Many times/day

2

16.67

2

16.67

Everyday

3

25.00

5

41.67

Most days

2

16.67

7

58.33

Some days

2

16.67

9

75.00

Never/almost Never

3

25.00

12

100.00

 

I would like to be able to- 

  1. correctly apply the correct format to the recoded variable - I also need to keep the original format
  2. do this efficiently for a bunch of variables 

Thanks,

Maggie

 

3 REPLIES 3
urban58
Quartz | Level 8

ignore my question, I now see the answer

Tom
Super User Tom
Super User

@urban58 wrote:

ignore my question, I now see the answer


What was the solution?

Tom
Super User Tom
Super User

Either attach the right format to the right variable.

Or recode the variable.  For your variable it looks a simple subtraction should do the recoding.

peace2 = 6 - peace;
format peace2 s. peace sr. ;

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