DATA Step, Macro, Functions and more

masking varables in all datasets

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

masking varables in all datasets

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.

 

 


Accepted Solutions
Solution
‎03-21-2018 11:16 AM
Super User
Posts: 9,890

Re: masking varables in all datasets

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 9,890

Re: masking varables in all datasets

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,846

Re: masking varables in all datasets

Posted in reply to KurtBremser

@KurtBremser 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';

Frequent Contributor
Posts: 76

Re: masking varables in all datasets

Posted in reply to KurtBremser
Not working
WARNING: Argument 1 to function VARNUM referenced by the %SYSFUNC or %QSYSFUNC macro function is
out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The
result of the operations have been set to a missing value.
NOTE: Var forename not exists in dsname

Thanks.
Solution
‎03-21-2018 11:16 AM
Super User
Posts: 9,890

Re: masking varables in all datasets

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 76

Re: masking varables in all datasets

Posted in reply to KurtBremser
Yes now working Thyanks.
Frequent Contributor
Posts: 76

Re: masking varables in all datasets

Posted in reply to KurtBremser
not working for numeric and dates.
Thanks,
S
Super User
Posts: 9,890

Re: masking varables in all datasets


@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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,846

Re: masking varables in all datasets


@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;

Capture.PNG

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 128 views
  • 0 likes
  • 3 in conversation