BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
data_null__
Jade | Level 19
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]
deleted_user
Not applicable
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!!
deleted_user
Not applicable
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.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 645 views
  • 0 likes
  • 3 in conversation