BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use the put() function:

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

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use the put() function:

data want;
	set have;
	test1=put(x,type.);
	test2=put(x,type.);
run;
SAS_inquisitive
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

SAS_inquisitive
Lapis Lazuli | Level 10

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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