DATA Step, Macro, Functions and more

changing the values of a large number of variables at the same time

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

changing the values of a large number of variables at the same time

Hi,

I have a dataset with a bunch of variables: a1-a20 (i.e. a1, a2, ... a20), b1-b10, c1-c10.

 

All of the variables have the same attributes (all numeric): 1 = yes, 2 = no, 3 = don't know.

 

Is there a quick way to change all the variables, such that whenever the value is 3, it is set to missing, for all variables in the dataset?

 

Thanks!

the dataset looks like this:

 

 

a1 a2 a3... b1

1   2   1  ...3

etc.


Accepted Solutions
Solution
‎01-17-2016 11:13 PM
Respected Advisor
Posts: 4,920

Re: changing the values of a large number of variables at the same time

Posted in reply to howdelicious

Your variable names cannot form numbered variable lists but you can simply use

 

array letters{*} a1_1 -- d10;
do i = 1 to dim(letters);
	if letters{i} = 3 then call missing(letters{i});
	end;

where a1_1 and d10 are respectively the first and last of your three-valued variables.

PG

View solution in original post


All Replies
Super User
Posts: 19,789

Re: changing the values of a large number of variables at the same time

[ Edited ]
Posted in reply to howdelicious

I'm not 100% sure what you're after, but you'll most likely be looking for an array, call missing() routine and whichn().

Something like this if I understand your requirements.

array letters(*) a1-a10 b1-b10 c1-c10;

if whichn(3, of letters(*))>0 then call missing (of letters(*));
Occasional Contributor
Posts: 6

Re: changing the values of a large number of variables at the same time

Hi Reeza,

Thanks for your reply. I am thinking of an array to change the values.

 

Essentially, I have several hundred numeric variables, where 1 = yes, 2 = no, 3 = don't know.

I wish to change all the '3's to missing.

 

Here are some examples of variable names that I have: 

a1_1-a1_8
b1_1-b1_9
b1n2_1-b1n2_7
c1n3_1-c1n3_9
d1-d10

The issue is that when I tried using an array, it wouldn't read in the variables "a1_1-a1_8". It comes up with an error saying:

Syntax error, expecting one of the following: an integer constant, *.

 

Solution
‎01-17-2016 11:13 PM
Respected Advisor
Posts: 4,920

Re: changing the values of a large number of variables at the same time

Posted in reply to howdelicious

Your variable names cannot form numbered variable lists but you can simply use

 

array letters{*} a1_1 -- d10;
do i = 1 to dim(letters);
	if letters{i} = 3 then call missing(letters{i});
	end;

where a1_1 and d10 are respectively the first and last of your three-valued variables.

PG
Occasional Contributor
Posts: 6

Re: changing the values of a large number of variables at the same time

[ Edited ]

*EDIT*

Actually, I coded in the array incorrectly before. Your solution worked perfectly. Thank you!!!

Super User
Posts: 19,789

Re: changing the values of a large number of variables at the same time

Posted in reply to howdelicious

Do you have any naming convention? If you don't, unfortunately the only way is to manually write them out.

Respected Advisor
Posts: 4,920

Re: changing the values of a large number of variables at the same time

Posted in reply to howdelicious

If your variables names appear sequentially in the dataset variable list then their names and order doesn't matter. The firstOne--lastOne list will include all variables in between those two, as long as they are of the same type (numeric or character).

PG
Super User
Posts: 19,789

Re: changing the values of a large number of variables at the same time

And if you want to explicitly specify numeric only

 

firstvar-numeric-lastvar

http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...

 

Occasional Contributor
Posts: 6

Re: changing the values of a large number of variables at the same time

Hi Reeza and PGStats, the thread has been solved thanks to PGStat's original post.

I mis-coded my array earlier, hence it didn't work. I realised my error and found that PGStat's solution worked perfectly. Thanks for your help!!
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 297 views
  • 3 likes
  • 3 in conversation