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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

8 REPLIES 8
Astounding
PROC Star

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.

Reeza
Super User

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;
kannand
Lapis Lazuli | Level 10

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

Kannan Deivasigamani
turcay
Lapis Lazuli | Level 10

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.

Reeza
Super User
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;
turcay
Lapis Lazuli | Level 10

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

andreas_lds
Jade | Level 19

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;

 

turcay
Lapis Lazuli | Level 10

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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