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

Hi guys,

 

I'm a new sas user and I wanna learn somethings from you.

 

is any possible that I retain the variables in the new variable as like the list, step by step, for example:

 

I have four variables as below:

dep = var1, var2, var3, var4

 

The Macro I wrote 

 

%do i=1 %to %sysfunc(countw(&dep));
%let dep_nxt = %scan(&dep, &i);
 
data dset_temp; set &dset; if &dep_nxt =. then delete; run;  *(I wanna clean each independent variables, not cleaning all at same time);
 
 

first times : new_dep = var1_name 

second times : new_dep= var1_name var2_name

third times : new_dep = var1_name var2_name var2_name

.

.

and there is a space between each name

 
 
thank you very much
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

As a new user, it looks like you are making some progress here.  But there are still issues that you need to grasp.  

 

Let's start with your original question.  If you want to collect names into &NEW_DEP, here is the idea of how you would do it:

 

%let new_dep=;

%do i=1 %to %sysfunc(countw(&dep));
   %let dep_nxt = %scan(&dep, &i);
   %let new_dep = &new_dep &dep_nxt;
%end;
Beyond that, though, here are a few issues you will need to contend with.
 
You claim that you wrote a macro.  But the code you posted isn't a macro.  There is no %MACRO statement, no %MEND statement.  There is no %END statement to match the %DO statement.  So you have pieces of a macro, but it isn't a macro yet.  Perhaps you posted only a few statements from your code?
 
Next, you are trying to go through some trouble to remove commas to get the equivalent of:
%let dep = var1, var2, var3, var4;
%let new_dep = var1 var2 var3 var4;
It would be much simpler (and probably just as easy as what you are doing now) to get rid of the commas when creating &DEP.
 
Next, consider what would happen if your macro actually worked.  SAS would see these statements:
 
data dset_temp; set &dset; if var1 =. then delete; run; 
data dset_temp; set &dset; if var2 =. then delete; run; 
data dset_temp; set &dset; if var3 =. then delete; run; 
data dset_temp; set &dset; if var4 =. then delete; run; 
Each time the %DO loop iterates, it reads from your incoming data set and REPLACES DSET_TEMP.  There is only one output data set, and the final contents of DSET_TEMP reflect VAR4 only.  You could easily account for this by generating the equivalent of:
data var1; set &dset; if var1 =. then delete; run; 
data var2; set &dset; if var2 =. then delete; run; 
data var3; set &dset; if var3 =. then delete; run; 
data var4; set &dset; if var4 =. then delete; run; 
Give the output data set a new name each time through the %DO loop.  And it's easy to do, since you already have a macro variable that contains such a name.  Just use:
data &dep_nxt; set &dset; if &dep_nxt =. then delete; run;
Finally, note that you are reading the incoming data set four times.  You could combine that into one DATA step and read the incoming data only once  It would require changing the generated SAS code to this.
data var1 var2 var3 var4;
set &dset;
if var1 > . then ouput var1;
if var2 > . then output var2;
if var3 > . then output var3;
if var4 > . then output var4;
run;
For now, I'll leave that as an exercise for you to solve, using macro language.
 
 
 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

I don't see any value in running the data step repeatedly. Do it in one run:

%let dep=var1 var2 var3 var4;

%macro delete_deps(indata=,outdata=,depvars=);

data &outdata.;
set &indata.;
%do i = 1 %to sysfunc(countw(&depvars.));
  %let dep_single=%scan(&depvar2.,&i.);
  if &dep_single. = . then delete;
%end;
run;

%mend;

%delete_deps(indata=xxxx,outdata=yyyy,depvars=&dep.)
andreas_lds
Jade | Level 19

What exactly do you mean by saying "I wanna clean each independent variables, not cleaning all at same time"?

 

The delete statement removes observations not variables.

RayZ
Calcite | Level 5

my English is not well 🙂

 

It means that I would delete the missing value in each variable(column) before I put the variable into the regression. ^^

 

Kurt_Bremser
Super User

You cannot "delete" a missing value, as it is already "nothing". What you can delete, are whole observations (=rows) that contain missing values, or you could drop variables (=columns) from the dataset altogether if you find they contain certain values (or only missing values).

What of these options you want to do will determine the code.

Astounding
PROC Star

As a new user, it looks like you are making some progress here.  But there are still issues that you need to grasp.  

 

Let's start with your original question.  If you want to collect names into &NEW_DEP, here is the idea of how you would do it:

 

%let new_dep=;

%do i=1 %to %sysfunc(countw(&dep));
   %let dep_nxt = %scan(&dep, &i);
   %let new_dep = &new_dep &dep_nxt;
%end;
Beyond that, though, here are a few issues you will need to contend with.
 
You claim that you wrote a macro.  But the code you posted isn't a macro.  There is no %MACRO statement, no %MEND statement.  There is no %END statement to match the %DO statement.  So you have pieces of a macro, but it isn't a macro yet.  Perhaps you posted only a few statements from your code?
 
Next, you are trying to go through some trouble to remove commas to get the equivalent of:
%let dep = var1, var2, var3, var4;
%let new_dep = var1 var2 var3 var4;
It would be much simpler (and probably just as easy as what you are doing now) to get rid of the commas when creating &DEP.
 
Next, consider what would happen if your macro actually worked.  SAS would see these statements:
 
data dset_temp; set &dset; if var1 =. then delete; run; 
data dset_temp; set &dset; if var2 =. then delete; run; 
data dset_temp; set &dset; if var3 =. then delete; run; 
data dset_temp; set &dset; if var4 =. then delete; run; 
Each time the %DO loop iterates, it reads from your incoming data set and REPLACES DSET_TEMP.  There is only one output data set, and the final contents of DSET_TEMP reflect VAR4 only.  You could easily account for this by generating the equivalent of:
data var1; set &dset; if var1 =. then delete; run; 
data var2; set &dset; if var2 =. then delete; run; 
data var3; set &dset; if var3 =. then delete; run; 
data var4; set &dset; if var4 =. then delete; run; 
Give the output data set a new name each time through the %DO loop.  And it's easy to do, since you already have a macro variable that contains such a name.  Just use:
data &dep_nxt; set &dset; if &dep_nxt =. then delete; run;
Finally, note that you are reading the incoming data set four times.  You could combine that into one DATA step and read the incoming data only once  It would require changing the generated SAS code to this.
data var1 var2 var3 var4;
set &dset;
if var1 > . then ouput var1;
if var2 > . then output var2;
if var3 > . then output var3;
if var4 > . then output var4;
run;
For now, I'll leave that as an exercise for you to solve, using macro language.
 
 
 
RayZ
Calcite | Level 5

Yes~~~!! Thank you very much.

I really wanna learn how to build it in sas ^^ 

I am very new in using SAS, and thanks for all of your kindly help.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 559 views
  • 2 likes
  • 4 in conversation