BookmarkSubscribeRSS Feed
sascodequestion
Fluorite | Level 6

Hi Friends,

 

I have a data set like below (input) and need your help in getting the output dataset.

 

data input;
input ColumnA $ ColumnB $ ColumnC $ ColumnD$ Value;
cards;
A B C A1 1
A B C A1 2
A B C A1 3
A B C A1 4
A B C A2 1
A B C A2 2
A B C A2 3
A B C A2 4
;

 

output:


data output;
input ColumnA $ ColumnB $ ColumnC $ A1 A2;
cards;
A B C 1 1
A B C 2 2
A B C 3 3
A B C 4 4
;

4 REPLIES 4
mkeintz
PROC Star

YOu have not sufficiently described the rules by which you want to associate the data records.

  1. Are you associating records within columna/columnb/columnc groups?
  2. Are you pairing columnd='A1' and columnd='A2' records?
  3. If so, are they associated by position within a/b/c?  
  4. Do you always expect the same number of A1's and A2's?

Please try to avoid mindreading requirements.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sascodequestion
Fluorite | Level 6

Hi,

Thanks for your response.

1.The total number of observations should be devided into 2 groups, which is based on the values of the coulmnD(A1 and A2)

2. The observations  should be grouped based on ColumnA,columnB, ColumnC and columnD

3. The Values in the columnD should become new coulmns, for example in the above example we have two different values A1 and A2 so we should get 2 new columns in the output called A1 and A2

3. Column A1 and A2  no need to have same rows. The total number of observation are going to be equal to the  higher number of observations in any group.  For example if group A1 contains 5 and A2 contains 3, so total number of obs will be equal to 5 and column A2 will have values 0.

 

Hope this is clear

art297
Opal | Level 21

You would have to create new variables to account for what you want. Given your example you could use something like:

 

data input;
input ColumnA $ ColumnB $ ColumnC $ ColumnD $ Value;
cards;
A B C A1 1
A B C A1 2
A B C A1 3
A B C A1 4
A B C A2 1
A B C A2 2
A B C A2 3
A B C A2 4
;

data need;
  set input;
  if mod(_n_,4) eq 0 then x=4;
  else x=mod(_n_,4);
run;

proc sort data=need;
  by ColumnA ColumnB ColumnC x ColumnD;
run;

data need;
  set need;
  if mod(_n_,2) eq 1 then group+1;
run;

proc transpose data=need out=want (drop= group _name_);
  by ColumnA ColumnB ColumnC Group;
  var value;
  id ColumnD;
run;

HTH,

Art, CEO, AnalystFinder.com

 

mkeintz
PROC Star

Assuming (1) your data are sorted by columna/columnb/columnc and (2) columnd='A1' or 'A2', then you can use a MERGE process as below:

 

data want (drop=n n1 n2 columnd);
  n1=0; n2=0;
  do until (last.columnc);
    set input (keep=columna columnb columnc columnd);
    by columna columnb columnc;
    if      columnd='A1' then n1=n1+1;
    else if columnd='A2' then n2=n2+1;
  end;

  do N=1 by 1 until (last.columnc);
    merge input (in=in1 where=(columnd='A1') rename=(value=a1))
          input (in=in2 where=(columnd='A2') rename=(value=a2));
    by columna columnb columnc;
    if n>n1 then a1=0;
    if n>n2 then a2=0;
    output;
  end;
run;

 

 

Note that N1 and N2 are the counts of A1 and A2 records respectively.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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