Help using Base SAS procedures

How to append variables?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How to append variables?

Hello,

The data I have is as follows:

IDScore1 Score2Score3
A528
B437
C961

I want to convert it as follows:

IDScore
A5
B4
C9
A2
B3
C6
A8
B7
C1

The way I'm doing it pretty crude:

data want;

     set have (keep = id score1 rename=(score1=score)

          have (keep = id score2 rename=(score2=score)

          have (keep = id score3 rename=(score3=score);


run;


This could be tedious with say 50 variables. Is there any sophisticated way? Can proc transpose handle this?


Thanks,

P


Accepted Solutions
Solution
‎03-11-2015 11:50 AM
Super User
Posts: 7,854

Re: How to append variables?

Then do the following:

data want (keep=id score);

set have;

array scores {} score1-score3; * or use _numeric_ if all numeric variables are to be taken for output;

do i = 1 to dim(scores);

  score = scores{i};

  output;

end;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 7,854

Re: How to append variables?

One option would be to create the list of input datasets automatically with a macro loop. proc transpose would (IMHO) not be able to create your A,B,C,A,B,C order. It would rather give you A,A,A,B,B,B,C,C,C instead.

If the order of output records doe not matter, usage of an array and looping through that would also come to mind.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: How to append variables?

Posted in reply to KurtBremser

Thanks for your reply. No, order is not important here.

Solution
‎03-11-2015 11:50 AM
Super User
Posts: 7,854

Re: How to append variables?

Then do the following:

data want (keep=id score);

set have;

array scores {} score1-score3; * or use _numeric_ if all numeric variables are to be taken for output;

do i = 1 to dim(scores);

  score = scores{i};

  output;

end;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: How to append variables?

Posted in reply to KurtBremser

Superb. Worked perfectly!

Super User
Super User
Posts: 7,988

Re: How to append variables?

Hi,

data have;

  infile datalines;

  input ID $ Score1 Score2 Score3;

datalines;

A 5 2 8

B 4 3 7

C 9 6 1

;

run;

data _null_;

  set sashelp.vcolumn (where=(libname="WORK" and memname="HAVE" and substr(name,1,5)="Score")) end=last;

  if _n_=1 then call execute('data want; set ');

  call execute(' work.have (keep=id '||strip(name)||' rename=('||strip(name)||'=Score))');

  if last then call execute(';run;');

run;

SAS Employee
Posts: 232

Re: How to append variables?

Hi - thanks for being part of Communities on SAS - I noticed this question was asked in the "about communities on SAS" community .. just for greater visibility I'm moving it to the SAS procedures community. Best regards!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 265 views
  • 5 likes
  • 4 in conversation