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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
PGStats
Opal | Level 21

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
NCiaccia
Fluorite | Level 6

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.                                     

PGStats
Opal | Level 21

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
NCiaccia
Fluorite | Level 6

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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