Bonjour, c'est la première fois que je post ici, alors j'espère l'avoir fait au bon endroit.
Voilà la partie du code qui me pose problème :
%macro locf(datasetin,datasetout,vars,byvar,visitvar );
%let i=1;
%do i=1 %to %words(&vars)
%let var=%scan(&vars,&i," ");
%let vartypes=%vtype(locfinitial,&var);
data dataset&i;
set &datasetin(keep=&byvar &visitvar &var
where=(&var <>
%if "&vartypes" = "C" %then %do;
' '
%end;
%else %do ;
.
%end;
));
run;
%end;
%mend;
%locf(test,final,Var1 Var2,id,visit);
Tout fonctionne parfaitement d'après moi, mais quand je loop, la macro variable "var" devrait contenir la variable "Var1" pour i=1 et "Var2" pour i=2. Et c'est là qu'est le probleme, var ne prend que "Var1", peu importe la valeur de i. Je me retrouve donc avec dataset1 qui contient id, visit et Var1 et mon dataset2 qui contient id, visit et Var2. J'ai essayé de tout vérifier et je pense que le soucis vient de ma fonction %scan que j'utilise pour définir "var".
Use the MISSING() function.
Move the %DO loop to the right place.
%macro locf(datasetin,datasetout,vars,byvar,visitvar );
%local i var ;
data dataset&i;
set &datasetin(keep=&byvar &visitvar &vars
where=(1=1
%do i=1 %to %sysfunc(countw(&vars," "));
%let var=%scan(&vars,&i," ");
and not missing(&var)
%end;
));
run;
%mend;
%locf(test,final,Var1 Var2,id,visit);
Use the MISSING() function.
Move the %DO loop to the right place.
%macro locf(datasetin,datasetout,vars,byvar,visitvar );
%local i var ;
data dataset&i;
set &datasetin(keep=&byvar &visitvar &vars
where=(1=1
%do i=1 %to %sysfunc(countw(&vars," "));
%let var=%scan(&vars,&i," ");
and not missing(&var)
%end;
));
run;
%mend;
%locf(test,final,Var1 Var2,id,visit);
I am guessing from that that you want, for a given dataset and a given set of variables that you want to set them to missing, be it character or numeric. None of that code is necessary. Use Base SAS always, only resort to macro for repeated code.
Can you follow the new post guidance and provide test data in the form of a datstep and what you want out at the end - this makes a problem so much clearer. For instance, why are you creating one dataset for each variable? Do you need some sort of summary of missing data items as proc freq or summary can do that much easier.
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.