BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

Let's say that I have a char column with format.

I know that using the sentance FORMAT VAR_NAME  will remove the format from the column and the column type will remain same (IF it was char then will stay char and if was numeric then will stay numeric).

My question:

What is the way to remove format via proc sql?

I know to use input(Var_NAME,best.)  and it remove the format and also convert it to numeric .

But I just want to remove the format (via proc sql) without change the field type.

What is the way to do it please?

Data t1;
input City $;
cards;
222
333
444
;
Run;

proc format ;
value $fmt
'222'='London'
'333'='Liverpool'
'444'='Leeds'
;
Run;

Data t2;
set t1;
format City $Fmt.;
Run;

Data want1a;
set t2;
format City;/**Remove format from column City: The field stay char**/
Run;


proc sql;
create table want1b as
select input(city,best.) as city /***Remove the format and create numeric field**/
from t2
;
quit;

 

5 REPLIES 5
andreas_lds
Jade | Level 19

Using the put function should work. But if you just want to remove a format, using proc datasets is recommended.

Kurt_Bremser
Super User

If you want to just show your three-digit codes, remove the format with PROC DATASETS and be done with it.

A numeric variable eats 8 bytes, while you can store the codes in a $ 3 variable. And you will never make calculations with the codes.

Ronein
Meteorite | Level 14

Thanks, so as far as I understand via proc sql we cannot remove format?(without change column type)

Kurt_Bremser
Super User

PROC DATASETS only changes the header page of a dataset, while PROC SQL needs to rewrite the whole dataset.

That's why PROC DATASETS is the right tool (Maxim 14).

Tom
Super User Tom
Super User

There is no direct syntax in PROC SQL to do what you can with the FORMAT statement in a data step or proc datasets.

 

You cannot just select it AS something else.  PROC SQL remembers the format attached.  But if you do some operation on the variable (hopefully something that does not actually change the value) then PROC SQL will "forget" the format (and LABEL and other attributes).

 

Example:

data have;
  set sashelp.class ;
  format age 4. ;
run;

proc sql ;
create table want as
  select *, age as New_age1, sum(age,.) as New_age2
  from have
;
quit;

proc contents data=want varnum;
run;

Tom_0-1668608217785.png

 

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
  • 5 replies
  • 971 views
  • 0 likes
  • 4 in conversation