DATA Step, Macro, Functions and more

How do I repeat a data step for all variables in a dataset with the same suffix

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How do I repeat a data step for all variables in a dataset with the same suffix

[ Edited ]

In my dataset, I have many variables that end with the suffix _RND. For each observation in these variables, if the value is less than 0, I would like it to be 0 and if the value is greater than 3, I would like it to be 3. I've gotten so far as to be able to identify all of the variables in the dataset that end with the suffix and stored it in a list called rounded_scores (using the code below in SAS 9.4), but I dont know how to invoke this list and have the procedure repeated on each of these variables.

 

proc contents data=Henry out=contents(keep=name) noprint ;
Run;

proc sql noprint ;
select name into :rounded_scores separated by ' '
from contents
where upcase(name) like '%^_RND' escape '^'
;
quit;

 

Thanks in advance for any help you can give!


Accepted Solutions
Solution
‎11-30-2016 12:42 PM
Respected Advisor
Posts: 4,644

Re: How do I repeat a data step for all variables in a dataset with the same suffix

To keep missing values as missing, all you need is:

 

data want;
set Henry;
array v{*} &rounded_scores;
do i = 1 to dim(v);
	if not missing(v{i}) then v{i} = max(0, min(3, v{i}));
	end;
drop i;
run;
PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,644

Re: How do I repeat a data step for all variables in a dataset with the same suffix

You have done most of the work. All that is missing is:

 

data want;
set Henry;
array v{*} &rounded_scores;
do i = 1 to dim(v);
	v{i} = max(0, min(3, v{i}));
	end;
drop i;
run;
PG
New Contributor
Posts: 3

Re: How do I repeat a data step for all variables in a dataset with the same suffix

Thanks so much for your response! That is super helpful. I am running into one problem. I have many blank observations in these variables and this array is turning anyone blank into a 3. I tried using if &rounded_score ne . in a few different places, both before the array and in the array, but I can't quite get the proper syntax to make it function.                                     

Solution
‎11-30-2016 12:42 PM
Respected Advisor
Posts: 4,644

Re: How do I repeat a data step for all variables in a dataset with the same suffix

To keep missing values as missing, all you need is:

 

data want;
set Henry;
array v{*} &rounded_scores;
do i = 1 to dim(v);
	if not missing(v{i}) then v{i} = max(0, min(3, v{i}));
	end;
drop i;
run;
PG
New Contributor
Posts: 3

Re: How do I repeat a data step for all variables in a dataset with the same suffix

Thank you! I also realized after I posted that I should be using v{i}. Whoops. I found that this would also work.

 

data want;

set Henry;

array v{*} &rounded_scores;

do i = 1 to dim(v);

if v{i} ne . then do;

v{i} = max(0, min(3, v{i}));

end;

end;

drop i;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 214 views
  • 0 likes
  • 2 in conversation