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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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