Hi All,
I want to mask the variables values with 'XXXX' in all datasets so I created a macro with masking variables. 'names' data set has all variables (name,title,name_s house_name house_number) but 'test'data set has only two variables .when I use this test macro for test table it is creating dummy varables and also remaining datasets doesn't have all the variables.
I used below code to check the varable existence but I am stuck here .if variables exits then mask else don't mask .(I need this kind of approach).
%macro VarExist(ds, var);
%local rc dsid result;
%let dsid = %sysfunc(open(&ds));
%if %sysfunc(varnum(&dsid, &var)) > 0 %then %do;
%let result = 1;
%put NOTE: Var &var exists in &ds;
%end;
%else %do;
%let result = 0;
%put NOTE: Var &var not exists in &ds;
%end;
%let rc = %sysfunc(close(&dsid));
&result
%mend VarExist;
%put %VarExist(names, name);
%macro test;
name=tranwrd(name, trim(name), "xxxxx");
title=tranwrd(title, trim(title), "xxxxx");
name_s=tranwrd(name_s, trim(name_s), "xxxxx");
house_name=tranwrd(house_name, trim(house_name), "xxxxx");
house_number=tranwrd(house_number, trim(house_number), "xxxxx");
%mend;
data a;
set names;
%test;
/*ss=cat(title,name);*/
run;
data b;
set test;
%test;
run;
sorry my question is not that clear.
Tahnks,
S.
Got it to work. See the whole code:
%macro VarExist(ds, var);
%local rc dsid result;
%let dsid = %sysfunc(open(&ds));
%if %sysfunc(varnum(&dsid, &var)) > 0 %then %do;
%let result = 1;
%put NOTE: Var &var exists in &ds;
%end;
%else %do;
%let result = 0;
%put NOTE: Var &var not exists in &ds;
%end;
&result
%mend;
%macro test(dsname,varname);
%if %varexist(&dsname,&varname) %then %do;
&varname = "xxxxx";
%end;
%mend;
%macro testall(dsname);
%test(&dsname,Name);
%test(&dsname,title);
%test(&dsname,name_s);
%test(&dsname,house_name);
%test(&dsname,house_number);
%mend;
data test;
set sashelp.class;
%testall(sashelp.class);
run;
Try this:
%macro test(dsname,varname);
%if %varexist(&dsname,&varname) %then %do;
&varname = tranwrd(&varname, trim(&varname), "xxxxx");
%end;
%mend;
%macro testall(dsname);
%test(dsname,name);
%test(dsname,title);
%test(dsname,name_s);
%test(dsname,house_name);
%test(dsname,house_number);
%mend;
data a;
set names;
%testall(names);
/*ss=cat(title,name);*/
run;
data b;
set test;
%testall(test);
run;
@Kurt_Bremser wrote:
Try this:
%macro test(dsname,varname); %if %varexist(&dsname,&varname) %then %do; &varname = tranwrd(&varname, trim(&varname), "xxxxx"); %end; %mend; %macro testall(dsname); %test(dsname,name); %test(dsname,title); %test(dsname,name_s); %test(dsname,house_name); %test(dsname,house_number); %mend; data a; set names; %testall(names); /*ss=cat(title,name);*/ run; data b; set test; %testall(test); run;
Why are you using TRANWRD? Just assign &varname='XXXXXX';
Got it to work. See the whole code:
%macro VarExist(ds, var);
%local rc dsid result;
%let dsid = %sysfunc(open(&ds));
%if %sysfunc(varnum(&dsid, &var)) > 0 %then %do;
%let result = 1;
%put NOTE: Var &var exists in &ds;
%end;
%else %do;
%let result = 0;
%put NOTE: Var &var not exists in &ds;
%end;
&result
%mend;
%macro test(dsname,varname);
%if %varexist(&dsname,&varname) %then %do;
&varname = "xxxxx";
%end;
%mend;
%macro testall(dsname);
%test(&dsname,Name);
%test(&dsname,title);
%test(&dsname,name_s);
%test(&dsname,house_name);
%test(&dsname,house_number);
%mend;
data test;
set sashelp.class;
%testall(sashelp.class);
run;
@sathya66 wrote:
not working for numeric and dates.
Thanks,
S
Of course not. "xxxxx" is a string. You need to set a rule how to anonymize numerics.
@sathya66 wrote:
not working for numeric and dates.
Thanks,
S
I think you can do this much more easily with this simple code.
data newhart;
if 0 then set sashelp.heart;
retain _character_ 'XXXXXXXXX' _numeric_ .M;
set sashelp.heart(drop=AgeCHDdiag Sex AgeAtStart Height Weight Diastolic Systolic MRW Smoking AgeAtDeath Cholesterol Chol_Status BP_Status);
run;
proc print;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.