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!
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;
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;
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.
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;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.