DATA Step, Macro, Functions and more

Repeat a process for multiple variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Repeat a process for multiple variables

[ Edited ]

Hello All,

 

I would like to repeat a process for 15 variables and I normally due this just my copying and pasting the code several times. I was wondering if an array or a macro could do the same thing?

 

I have two things that I would like to make more 'compact"

 

 for variables pre_var1-15

 

1: make certain partions missing based on a conditional

 

if test = 0 then pre_var1_v1 = .;
if test in(0,1) then pre_var1_v2 = .;

 

if test in(0,1) then pre_var1_v3 = .;

 

2:  If data is missing fill in with previous data

if var1_F = . and time <= 25 then var1_F = pre_var1_v1 ;
if var1_F = . and time <= 25 then var1_F = pre_var1_v2 ;
if var1_F = . and time <= 25 then var1_F = pre_var1_v3 ;

 

Any suggestions would be great,

 

Thank you

Jack

 

EDIT:

I have edited the above text in bold. 

 

When the variable test is 0, I would like to make (pre_var1_v1 = .)  and when test = 0 or test = 1 then I would like to make (pre_var1_v2 = .). For the sake of simplicity, I took out the pre_var1_v3 variable.

 

I took out the second part, I believe if I can get a start on how to build the macro/array in the first part, I can figure out how to code it.

 

As per request, attached is the before/ after of the data:

 

data have;
input id test time pre_var1_v1 pre_var1_v2 pre_var1_v3;
cards;
1 0 0 1.5 1.6 1.4
1 1 5 0.5 1.5 1.6
1 2 10 0.9 0.5 1.5
1 3 15 0.7 0.9 0.5
1 4 20 0.6 0.7 0.9
2 0 0 1.5 1.6 1.4
2 1 5 0.5 1.5 1.6
2 2 10 0.9 0.5 1.5
2 3 15 0.7 0.9 0.5
2 4 20 0.6 0.7 0.9
;;;;;
run;

data want;
input id test time pre_var1_v1 pre_var1_v2;
cards;
1 0 0 . .
1 1 5 0.5 .
1 2 10 0.9 0.5
1 3 15 0.7 0.9
1 4 20 0.6 0.7
2 0 0 . .
2 1 5 0.5 .
2 2 10 0.9 0.5
2 3 15 0.7 0.9
2 4 20 0.6 0.7
;;;;;
run;

 


Accepted Solutions
Solution
‎03-21-2017 03:16 PM
Occasional Contributor
Posts: 7

Re: Repeat a process for multiple variables

Posted in reply to jckbnimble

I figured it out with the help of this article.

 

Array test(32) /*1-5(5) + 6-10(5) + 11-15(5) = 15
/*  1         2           3         4          5  */
prevar1_v1 prevar2_v1 prevar2_v1 prevar2_v1 prevar2_v1 /*n = 5*/
/*  6         7           8         9         10  */
prevar1_v2 prevar2_v2 prevar2_v2 prevar2_v2 prevar2_v2 /*n = 5*/
/* 11        12          13        14         15  */
prevar1_v2 prevar2_v2 prevar2_v2 prevar2_v2 prevar2_v2 /*n = 5*/;
Do i = 1 to 15;
if test in(0) then test{i} = . ; 
End; 

Do i = 6 to 15;
if test in(1) then test{i} = . ; 
End;

Do i = 11 to 15;
if test in(2) then test{i} = . ; 
End;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Repeat a process for multiple variables

Posted in reply to jckbnimble

You really need to supply some example data of your input data set and what the final result should be. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

What to do if missing value is on the first row?

What if time is missing? Missing is always less than 25 (or anything else for that matter) so confirm that is when you need to implement this.

 

You say to do this for Var1-15 but then never mention where Var1 is used/considered.

And this only makes sense if whatever pre_var_v1 and pre_var_v2 are missing. Is that your intent to go through a list of variables and assign the first non-missing value??

if var1_F = . and time <= 25 then var1_F = pre_var1_v1 ;
if var1_F = . and time <= 25 then var1_F = pre_var1_v2 ;
if var1_F = . and time <= 25 then var1_F = pre_var1_v3 ;

It is not clear if your pre_var1_v1 etc variables even exist or not. So the example datastep generated by the above instructions will clear up all questions about your starting data.

 

Solution
‎03-21-2017 03:16 PM
Occasional Contributor
Posts: 7

Re: Repeat a process for multiple variables

Posted in reply to jckbnimble

I figured it out with the help of this article.

 

Array test(32) /*1-5(5) + 6-10(5) + 11-15(5) = 15
/*  1         2           3         4          5  */
prevar1_v1 prevar2_v1 prevar2_v1 prevar2_v1 prevar2_v1 /*n = 5*/
/*  6         7           8         9         10  */
prevar1_v2 prevar2_v2 prevar2_v2 prevar2_v2 prevar2_v2 /*n = 5*/
/* 11        12          13        14         15  */
prevar1_v2 prevar2_v2 prevar2_v2 prevar2_v2 prevar2_v2 /*n = 5*/;
Do i = 1 to 15;
if test in(0) then test{i} = . ; 
End; 

Do i = 6 to 15;
if test in(1) then test{i} = . ; 
End;

Do i = 11 to 15;
if test in(2) then test{i} = . ; 
End;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 171 views
  • 0 likes
  • 2 in conversation