Hello All,
I have a dataset with groups of variables obtained from different files. The structure is as follows (note: variable names might vary):
Country_File1 Country_File2 Country_File3 Zip_File1 Zip_File2 Name_File1 Name_File2
Norway India 56698 Andrew Justin
Sweden Russia Denmark 8JH87 John
I need to create a new flag variable in the file based on the missing values of the above variables as follows:
Missing_Group
Country_File3, Zip_File2
Name_File1,Zip_FIle1
I know the result can be obtained using several (need about 25) IF THEN statements and CATX, but was looking for a simpler way.
Thanks for your help.
Why not ARRAY ?
data have;
input (Country_File1     Country_File2     Country_File3     Zip_File1     Zip_File2     Name_File1     Name_File2) ($);
cards; 
Norway               India                  .                          56698       .                       Andrew          Justin
Sweden            Russia               Denmark                .                 8JH87             .                    John
;
run;
data want;
 set have;
 length Missing_Group $ 400;
 array x{*} $ _character_;
 do i=1 to dim(x);
  if missing(x{i}) then Missing_Group=catx(',',Missing_Group,vname(x{i}));
 end;
 drop i;
run;
  
Xia Keshan
Why not ARRAY ?
data have;
input (Country_File1     Country_File2     Country_File3     Zip_File1     Zip_File2     Name_File1     Name_File2) ($);
cards; 
Norway               India                  .                          56698       .                       Andrew          Justin
Sweden            Russia               Denmark                .                 8JH87             .                    John
;
run;
data want;
 set have;
 length Missing_Group $ 400;
 array x{*} $ _character_;
 do i=1 to dim(x);
  if missing(x{i}) then Missing_Group=catx(',',Missing_Group,vname(x{i}));
 end;
 drop i;
run;
  
Xia Keshan
Hi Xia,
Thank you for the logic. It seems to work. But the dataset I have has about 200 other variables. I just want to consider the ones I showed her for flags. Wouldn't the array test for missing values of all variables? How do I limit it to testing only the few variables?
Thanks again,
Hi Xia,
Got it, should I just name the variables I want instead of _CHARACTER_ ?
Thanks,
Yes. List the variables you need at here :
array x{*} $ a b c d ;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.