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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

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;
data_null__
Jade | Level 19

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

sathya66
Barite | Level 11
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.
Kurt_Bremser
Super User

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
Barite | Level 11
Yes now working Thyanks.
sathya66
Barite | Level 11
not working for numeric and dates.
Thanks,
S
data_null__
Jade | Level 19

@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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1249 views
  • 0 likes
  • 3 in conversation