BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sami1234
Fluorite | Level 6

Dear Experts,

 

Would there be any way or function to apply the user-defined format to the variable if exists/present in the dataset? for example something like below.

 

Data test1;

set  test;

if var1 exists then do;

format var1 format1;

end;

if var2 exists then do;

format var2 format2;

end;

if var3 exists then do;

format var3 format3;

end;

run;

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

If you only want to assign formats, without other changes to the data, use proc datasets.

Set up a control table:

data control;
input varname :$32. fmtname :$32.;
datalines;
var1 format1.
var2 format2.
var3 format3.
;
run;

Check which variables are present:

proc sql;
create table correction as 
select a.varname, a.fmtname
from control a, dictionary.columns b
where upcast(a.varname) = upcast(b.name) and
  b.libname = "WORK" and b.memname = "TEST";
quit;

Dynamically create the code for proc datasets:

data _null_;
set correction end=eof;
if _n_ = 1 then call execute("
  proc datasets library=WORK noprint;
  modify TEST;
  format 
");
call execute(catx(" ",varname,fmtname, ""));
if eof then call execute("
  ;
  quit;
");
run;

View solution in original post

5 REPLIES 5
SASKiwi
PROC Star

The FORMAT statement is not an executable statement so you would have to use macro code to write this statement if the variable exists or not write it at all if not.

Sami1234
Fluorite | Level 6

Thanks for replying.

Would you please give me a little example?

Astounding
PROC Star
Maybe, in some cases. For example:

format religion: relfmt.;

This statement affects all variable names that begin with "religion". If there are none, it affects no variables but might issue a note or even a warning. If there is 1 named "religion" you get the right result. But if there are multiple such variables you get the wrong result.
Kurt_Bremser
Super User

If you only want to assign formats, without other changes to the data, use proc datasets.

Set up a control table:

data control;
input varname :$32. fmtname :$32.;
datalines;
var1 format1.
var2 format2.
var3 format3.
;
run;

Check which variables are present:

proc sql;
create table correction as 
select a.varname, a.fmtname
from control a, dictionary.columns b
where upcast(a.varname) = upcast(b.name) and
  b.libname = "WORK" and b.memname = "TEST";
quit;

Dynamically create the code for proc datasets:

data _null_;
set correction end=eof;
if _n_ = 1 then call execute("
  proc datasets library=WORK noprint;
  modify TEST;
  format 
");
call execute(catx(" ",varname,fmtname, ""));
if eof then call execute("
  ;
  quit;
");
run;
Sami1234
Fluorite | Level 6

Thank you so much, it works perfectly fine. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 754 views
  • 1 like
  • 4 in conversation