Macro %Squeeze
1. Description
The macro SQUEEZE will remove the unwanted spaces from character variable.
2. Dependencies
2.1 Global Macro Variables - N/A
2.2 Global Macros - N/A
3. Macro Parameters
Macro parameter | Description | Example |
---|---|---|
DATA (R) | The name of the input dataset with library. If dataset is located in the work library then give the dataset name only. | DATA=LB ,Default value: None |
LIB (O) | Name of the output library to which the squeezed dataset is to be placed. | LIB=WORK ,Default value: WORK |
(R): Required (O): Optional
4. Assumptions
- The macro expecting that the variables and dataset have the labels.
5. Sample Call
%squeeze(data,lib=work)
6. Macro Code
%macro squeeze(data,lib=work);
%if &data eq %then %do;
%put ISSUE FOUND: Macro parameter 'data' is not specified;
%end;
%else %do;
%if %scan(&data.,2,".") = %then %do;
%let data = %str(work.&data.);
%end;
%*--- Test case: checking dataset exist;
%if %sysfunc(exist(&data)) %then %do;
%let libpath = %sysfunc(pathname(&lib.));
%*--- Test case: if invalid library specified;
%if %sysfunc(libref(&lib)) ne 0 %then %do;
%put ISSUE FOUND: Invalid library specified;
%end;
%else %do;
%*--- For finding the order of variable in metadata;
proc contents data=&data out=order noprint varnum;
run;
proc sort data=order;
by varnum;
run;
data __meta;
set &data;
seqid=_n_;
run;
%let nobs = &sysnobs.;
%*--- Checking whether the dataset contains Character variables and also checking
the case when data contains only numeric varaiables;
data dummy; set order; where type=2;run;
%let dummyobs = &sysnobs.;
%if &dummyobs. >= 1 %then %do;
proc sql noprint;
select strip(name)||"="||'"'||strip(label)||'"' into:lbl separated by " "
from order
where type=2;
select name, memlabel,varnum into:odr separated by " ", :dlabel, :num
from order
order by varnum;
quit;
%*--- Test case: checking whether the dataset have atleast one observation;
%if &nobs. >= 1 %then %do;
%let del = __meta __meta_t;
proc transpose data = __meta out=__meta_t name=variable;
by seqid ;
var _character_;
run;
proc sql;
create table length as
select distinct variable, max(length(col1)) as length
from __meta_t
group by variable;
quit;
%end;
%*--- Test case: checking whether the dataset have no observation;
%else %if &nobs. < 1 %then %do;
%let del = __meta;
data length(keep=variable length);
set order;
length = 1;
where type=2;
rename name = variable;
run;
%end;
%*--- Assigning length for character variables;
proc sql noprint;
select strip(variable)||" $"||strip(put(length,best.)) into: base separated by " "
from length;
quit;
proc contents data=__meta out=mt_ct noprint;
run;
proc sql noprint;
select distinct name into:allvar separated by " "
from mt_ct;
select distinct name into:vars separated by " "
from mt_ct
where type= 2;
select distinct strip(strip(name)||"_") into:renam separated by " "
from mt_ct
where type= 2;
quit;
%*--- Renaming char variables;
data rename;
set __meta;
rename %do i=1 %to %sysfunc(countw(&vars.)); %scan(&vars.,&i," ")=%scan(&renam.,&i," ") %end; ;
run;
%if "&dlabel" eq "" %then %do;
%let label_ = ;
%end;
%else %if "&dlabel" ne "" %then %do;
%let label_ = %str(label= "&dlabel.");
%end;
%*--- Assigning new lengths for character variables;
data &lib..%scan(&data.,2,'.')(&label_. drop=seqid);
retain &odr.;
length &base.;
set rename;
%do i= 1 %to %sysfunc(countw(&vars.));
%scan(&vars.,&i," ")=%scan(&renam.,&i," ") ;
%end;
label &lbl.;
keep &allvar.;
run;
proc delete data= order &del mt_ct rename dummy;
quit;run;
%end;
%else %do;
%let _lib=%scan(&data.,1,".");
%let dsn=%scan(&data.,2,".");
proc copy in=&_lib. out=&lib. memtype=data; select &dsn.;run;
proc delete data= order __meta dummy;quit;run;
%end;
%end;
%end;
%else %do;
%put ISSUE FOUND: Invalid dataset specified;
%end;
%end;
%mend squeeze;