BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rammed
Calcite | Level 5

Hi All,

 

I am pretty new to SAS and would like to change the format of my data in a below way.

I could not understand how the available codes work., so your help will be much appreciated. 

 

obs.  var1 var2 var3 var4 var5

1        3      4      4      3      2

2        4      1      2      4      5

3        5      7      8      9      6 

4        1      9      3      6      4

 

I want the long dataset in way that each observation will have two rows. var 1 to 3 are the same for both rows. The value of var4 will come in first row and the value of var 5 will be in the second row of each observation.Then  a new variable should be created showing 0 where var4 and 1 where var 5. So the long dataset should be like below:

 

obs.  var1 var2 var3    var4-5     new_var

1        3      4      4          3              0

1        3      4      4          2              1

2        4      1      2          4              0

2        4      1      2          5              1

3        5      7      8          9              0

3        5      7      8          6              1

4        1      9      3          6              0

4        1      9      3          4              1

 

Thanks a lot

Rama

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data want;
set have;

var4_5=var4;  new_var=0;
output;
var4_5=var5;  new_var=1;
output;

drop var4 var5;
run;

Don't really need the array for such a simple problem. 

View solution in original post

6 REPLIES 6
HB
Barite | Level 11 HB
Barite | Level 11

Why on earth would you want to do this?

 

Maybe you could tell us the problem you are really trying to solve, I don't think this is it.  I'm guessing you think this data reorg will solve another problem we don't know about.  My guess is this data reorganization is not the solution to that other problem. 

 

http://xyproblem.info/

 

 

Rammed
Calcite | Level 5
You are right. Actually I am doing difference-in-differences model. There are two ways to do that: one using Proc Glm that can be performed with wide-format data. another way is using proc mixed that needs long format of data. I knew that the results should be similar. However, because i did not get good result with proc glm, I though just in case is good to try proc glm, that is why I asked for that.
Thanks
ballardw
Super User

Step 1: Provide data in the form of a data step. As it is we do not know if OBS is actually a variable in your data. I am going to assume not.

data have;
  input var1 var2 var3 var4 var5;
datalines;
 3      4      4      3      2
 4      1      2      4      5
 5      7      8      9      6 
 1      9      3      6      4
;
run;

since you want to do sort of count may as well use a data step:

 

data want;
   set have;
   array v var4 var5;
   do i= 1 to dim(v);
      var4_5= v[i];
      new_var= i-1;
      output;
   end;
   drop var4 var5 i;
run

 

Note that if you have more variable to string out (var4 and var5) adding them to the array and drop statement this approach would count each added variable as well.

novinosrin
Tourmaline | Level 20
data have;
  input var1 var2 var3 var4 var5;
datalines;
 3      4      4      3      2
 4      1      2      4      5
 5      7      8      9      6 
 1      9      3      6      4
;
run;

data want;
set have;
array t(*) var:;
var4_5=t(4);
new_var=0;
output;
var4_5=t(5);
new_var=1;
output;
drop var4 var5;
run;
Reeza
Super User
data want;
set have;

var4_5=var4;  new_var=0;
output;
var4_5=var5;  new_var=1;
output;

drop var4 var5;
run;

Don't really need the array for such a simple problem. 

Rammed
Calcite | Level 5
Thanks for this easy way.

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
  • 6 replies
  • 1844 views
  • 9 likes
  • 5 in conversation