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

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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);
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1023 views
  • 1 like
  • 3 in conversation