Help using Base SAS procedures

Assigning data

Reply
N/A
Posts: 0

Assigning data

Hello Everyone:

I have a dataset in this format.

a b a1 a2 a3 a4
1 0 2 4 6 8
2 1 3 1 5 7
3 1 2 4 6 7
4 0 2 3 1 5
5 0 1 4 3 2
6 1 2 5 7 9
7 0 8 5 3 2
8 1 3 6 7 2
9 0 1 5 3 2
10 1 7 9 2 1
;

Please note that for any observation (row), a is not equal to a1, a2, a3, and a4. Basically what I want do is to assign 0/1 to the variables a1-a4 based on variable 'a''s association with b. So for example, when a = 3, b = 1. Now, I want a value of 1 to be assigned to variables a1-a4 whose value anywhere in the dataset is equal to 3. When a1 = 7 (10th observation), then the value of 7 needs to be replaced by 0 since when a = 7 b = 0, and so forth. Any suggestion will be greatly appreciated. Thank you.
Super Contributor
Super Contributor
Posts: 3,174

Re: Assigning data

Posted in reply to deleted_user
For convenience in coding within a DATA step, you can setup an array defined for the range of variables a1-a4 and have a SAS code construct like shown below to assign any of the array variable based on "a" value:

DO X=1 to DIM(your_array);

END;

For the other instances where you are assigning values, simply code an IF THEN ; within the same DATA step.

Here is a link to the SAS support http://support.sas.com/ where you can find SAS-hosted documentation on the ARRAY and other SAS Language references. Also, there are technical conference papers and supplemental SAS examples on the site for general use and reference, using the SEARCH facility.

Reading Raw Data:
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a001112330.htm



ARRAY processing references:

http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002299816.htm

http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a000739610.htm



Scott Barry
SBBWorks, Inc.
Respected Advisor
Posts: 3,799

Re: Assigning data

Posted in reply to deleted_user
You can use a format to achieve this goal. If you want to actually replace the values of A1-A4 then use an array and [pre]input(put(array[index],a2b.),f8.)[/pre] or similar.

[pre]
data a;
input
a b a1 a2 a3 a4;
cards;
1 0 2 4 6 8
2 1 3 1 5 7
3 1 2 4 6 7
4 0 2 3 1 5
5 0 1 4 3 2
6 1 2 5 7 9
7 0 8 5 3 2
8 1 3 6 7 2
9 0 1 5 3 2
10 1 7 9 2 1
;;;;
run;

data cntl;
retain fmtname 'a2b' type 'N';
set a;
start = a;
label = b;
run;
proc format cntlin=cntl;
select a2b;
run;
proc print data=a;
format a1-a4 a2b.;
run;
[/pre]
N/A
Posts: 0

Re: Assigning data

Posted in reply to deleted_user
Scott, I tried to assign numbers just using arrays but it never really worked even after spending considerable time. But the proc format seems to work pretty good. Thanks to you both for great suggestions!!
N/A
Posts: 0

Assigning data

Posted in reply to deleted_user
Hi all,

Is there a way to deal with repetitve data in PROC FORMAT?

For example:
month a b a1 a2 a3 a4
10 1 0 2 4 3 5
10 2 1 3 1 5 4
10 3 1 2 4 5 1
10 4 0 2 3 1 5
10 5 0 1 4 3 2
11 1 1 2 5 4 3
11 2 0 1 5 3 4
11 3 1 2 4 5 1
11 4 0 1 5 3 2
11 5 1 4 3 2 1
........
;

Similar to my previous post, I need the value of "b", that is associated with a particular "a" value per each month, to replace any a1-a4 value (when a1-a4 equal a) for that month. I got an error saying that values overlap when I ran proc format. Thank you.
Ask a Question
Discussion stats
  • 4 replies
  • 133 views
  • 0 likes
  • 3 in conversation