BookmarkSubscribeRSS Feed
Rickyboy
Calcite | Level 5

Hi Guys

 

it s my first post here

I m a newbie and I m stuck on a problem 

I need to reshape a dataset so that duplicate rows in the old are moved to a single row with twice the column lengths in the new dataset

 

for example

 

A B C

A D E 

F G H

F I J                  becomes    A B C A D E

                                            F G H F I J 

how would I go about it ?

even better is there a way to do this in enterprise guide ?

 

Thanks for your help

6 REPLIES 6
Shmuel
Garnet | Level 18

Is the first variable the ID - the indication up to which observation to gathr the data ?

If yes - why to save it twice on the row ?

 

What would you do if there are 3 or 4 or more observations for the same ID?

 

In many cases, sas can deal better and easier with long form, as you have,

than with the wide form that you want.

 

 

Ksharp
Super User

It is easy for IML code.

 

data have;
input x1 $ x2 $ x3 $ ;
cards;
A B C
A D E 
F G H
F I J 
;
run;
proc iml;
use have;
read all var _char_ into x;
close;
want=cshape(x,2,0,1);
print want;
quit;
Rickyboy
Calcite | Level 5
Thanks on that case I do I create a computed column that takes the
difference between two rows providing the label in the first column matches.
something like
If A1=a2 then c1-c2 else 0
I have found it difficult to perform operations between rows so I thought
expanding will make it easier but I m new sas
Ksharp
Super User

Post some sample data and output that would be better to explain your question.

If you are doing PROC REPORT, I am afraid that you can't do it with PROC REPORT.

Rickyboy
Calcite | Level 5

The data I have is something like this

 

 

USERID          INCOME       DATE

Rick                12.000            march 2015

Rick                 13.000           September 2016

Mario               15.000          February 2013

Mario                16.000          June 2015

Mario               17.000           August 2017

Frank               20.000           June 2015

Frank               25.000           March 2016

 

I would like to compute a column that takes 13.000 - 12.000 = 1000 for rick and tells me if possible the amount of time it passed

same for Mario 1000 and 1000 (or 2000)   and Frank 5000   in 8 months

 

this is what I m trying to do

that s why I tried to spread the data to add more column

 

this is quite easy in excel but I m stuck on SAS

 

thanks for your help

 

 

Shmuel
Garnet | Level 18

 

   Assuming the data is sorted by USERID  and date is a sas numeric date

    then the next code may do what you want:

data want;
 set have;
  by userid;
       retain start_income start_date;
       if first.userid then do;
          start_income = income;
          start_date = date.
       end;
      
      if last.userid then do;
         delta_income = income = start_income;
         delta_date = intck('month', date, start_date);
          output;
      end;
      keep userid delta_income delta_date;
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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1077 views
  • 0 likes
  • 3 in conversation