DATA Step, Macro, Functions and more

How to - Change Format of Variable(Show Both Numeric and Character)

Accepted Solution Solved
Reply
Super Contributor
Posts: 381
Accepted Solution

How to - Change Format of Variable(Show Both Numeric and Character)

Hello everyone,

I have a dataset and I need to show one column as both numeric and character. I added a simple dataset. Actually, I tried some methods and I created it but I'm not sure am I using the right method.What "library=sasuser" provides different form empty proc format.

 

data have;
Length Column1 8 Column2 8;
Infile Datalines missover dlm=",";
Input Column1 Column2;
datalines;
1,0
2,0
3,0
4,0
1,1
2,1
3,1
4,1
1,2
2,2
3,2
4,2
;
Run;
proc format library=sasuser;
	value Column
	0='G(1)'
	1='G(2)'
	2='G(3)';
run;
proc format ;
	value Column
	0='G(1)'
	1='G(2)'
	2='G(3)';
run;

Proc sql;
Create table Want As
Select Column1
		,Column2 format=Column.
From Have;
Quit;

want.png

 

Thank you.


Accepted Solutions
Solution
‎12-01-2015 12:32 PM
Super User
Posts: 17,775

Re: How to - Change Format of Variable(Show Both Numeric and Character)

Use proc datasets to apply the format, this doesn't create a new dataset and doesn't process the dataset so it's efficient. Your underlying variable remains numeric, but has the format applied so it's viewed as G(1), G(2). If you use the method above, you create a new data set with a character variable. Up to you which method you use.

proc datasets lib=work nodetails nolist;
modify have;
format column2 column.;
run;quit;

View solution in original post


All Replies
Super User
Posts: 5,079

Re: How to - Change Format of Variable(Show Both Numeric and Character)

Your simplest solution would be to add this statement to your DATA step following the INPUT statement:

 

column3 = column2;

 

Then you could select column1 and column2 (with no formats) and column3 (with the format applied).

 

Good luck.

Super User
Posts: 17,775

Re: How to - Change Format of Variable(Show Both Numeric and Character)

[ Edited ]

You can't. A column can be either numeric or character. If you need to display both types of information, the default becomes character. If you need to show both the formatted and unformatted value of a variable, then you need two variables. One is the raw data and the second either has the variable with a format attached OR you can convert it to the character variable with the put function:

Proc sql;
Create table Want As
Select Column1, Column2, put(Column2, Column.) as Column3 label="Column2 Formatted"
From Have;
Quit;
Regular Contributor
Posts: 161

Re: How to - Change Format of Variable(Show Both Numeric and Character)

I agree with @Reeza . Did you mean to ask if a column can be read into 2 different variables instead ?

Kannan Deivasigamani
Super Contributor
Posts: 381

Re: How to - Change Format of Variable(Show Both Numeric and Character)

[ Edited ]

Hello @Reeza,

 

Thank you for the detailed information.

Actually, I need to make changes on Column2 variable. I don't want to make some changes on dataset and don't want new variable. I try to show variable as G(1), G(2) and G(3). I mean changes on view of variable.I'm not sure ,maybe I can overwrite the Column2 by using your method.

 

/*Method of Reeza*/
Proc sql; Create table Want As Select Column1,put(Column2, Column.) as Column2 label="Column2 Formatted" From Have; Quit;

Thank you.

Solution
‎12-01-2015 12:32 PM
Super User
Posts: 17,775

Re: How to - Change Format of Variable(Show Both Numeric and Character)

Use proc datasets to apply the format, this doesn't create a new dataset and doesn't process the dataset so it's efficient. Your underlying variable remains numeric, but has the format applied so it's viewed as G(1), G(2). If you use the method above, you create a new data set with a character variable. Up to you which method you use.

proc datasets lib=work nodetails nolist;
modify have;
format column2 column.;
run;quit;
Super Contributor
Posts: 381

Re: How to - Change Format of Variable(Show Both Numeric and Character)

Thank you @Reeza,

 

I think your reply is just I want. Thank your for the information.

I want to show data on dataset because I may use dataset to create another dataset but nice to know that, thank you @andreas_lds.

 

Thank you @kannand and @Astounding

Super Contributor
Posts: 259

Re: How to - Change Format of Variable(Show Both Numeric and Character)

Have you tried proc report?

 

proc report data=work.have;
   columns Column1 Column2 Column2=Column2String;

   define Column1 / display;
   define Column2 / display;
   define Column2String / display format=Column. "Column2";

run;

 

Super Contributor
Posts: 381

Re: How to - Change Format of Variable(Show Both Numeric and Character)

Hello @Reeza,

 

I just wonder why it brings G(1) instead of G(1) THK when we use put function.I want to see space characters too. Have you got an idea ?

 

Thank you.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 552 views
  • 1 like
  • 5 in conversation