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
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
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;
%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.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;
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.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.)
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.
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. ^^
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.
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;
%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.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;
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.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.
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!
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.