DATA Step, Macro, Functions and more

keeping original and formatted value together

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

keeping original and formatted value together

I am trying to put original variable and formatted variable values together.  This does not work since I have to format the original value. Is there a way to get around this?

proc format;
	value  type
		1="good"
		2="bad";
run;

data have;
	input x;
	cards;
 1
 2
 ;

data want;
	set have;
	format x type.;
	y='x';
	test1=vvalue(x);
	test2=vvaluex(y);
run;

Desired output:

x     test1 test2

1    good  good

2    bad    bad


Accepted Solutions
Solution
‎02-24-2016 02:45 PM
Super User
Super User
Posts: 7,401

Re: keeping original and formatted value together

Use the put() function:

data want;
	set have;
	test1=put(x,type.);
	test2=put(x,type.);
run;

View solution in original post


All Replies
Solution
‎02-24-2016 02:45 PM
Super User
Super User
Posts: 7,401

Re: keeping original and formatted value together

Use the put() function:

data want;
	set have;
	test1=put(x,type.);
	test2=put(x,type.);
run;
Regular Contributor
Posts: 234

Re: keeping original and formatted value together

@RW9  Thank you. I guess I was thinking hard.

Super User
Super User
Posts: 6,499

Re: keeping original and formatted value together

Make a new copy of the format where the decode includes the codes.  So

value type
 1="good"
 2="bad"
;

Is converted to 

value type
 1="1 good"
 2="2 bad"
;

Personally I have a utility macro that will convert a format catalog. So if I had a catalog name MYLIB.FORMATS I can run the macro and generate a catalog MYLIB.CFORMATS.  Then I can just change the FMTSEARCH option.  So when I am writing my program and debugging I can use the CFORMATS version and see the code so I know how to write my IF and WHERE statements.  Then when I produce the final report I can use the regular format catalog and the numbers do not appear.

Super User
Super User
Posts: 7,401

Re: keeping original and formatted value together

Yep, another options is to not use formats at all, put code and decode in a dataset.  CDISC has this kind of approach as its XML based, and it would be my preferred approach to avoid proprietary file formats, which as shown by the debacle of 32 and 64 bit catalogs goes to show.

Regular Contributor
Posts: 234

Re: keeping original and formatted value together

@Tom @RW9.  Thank you. Is there an example that I can follow if possible?

Super User
Super User
Posts: 7,401

Re: keeping original and formatted value together

Here is one example from my side, you could also do merging, i.e. merge on code=x which would be less resource hunry on bigger datasets as in the second example:

data codes;
  code=1; decode="good"; output;
  code=2; decode="bad"; output;
run;

data have;
	input x;
	cards;
 1
 2
;
run;

proc sql;
create table WANT as
select X,
(select THIS.DECODE from CODES THIS where THIS.CODE=X) as DECODED_VALUE
from HAVE;
create table WANT2 as
select A.X,
B.DECODE as DECODED_VALUE
from HAVE A
left join CODES B
on A.X=B.CODE;
quit;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 241 views
  • 2 likes
  • 3 in conversation