DATA Step, Macro, Functions and more

SAS PROGRAMMING QUESTION

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

SAS PROGRAMMING QUESTION

Please help me in explaning the answer of following program.

 

data a;

input id;

cards;

1

2

3

4

;

run;

 

data b;

set a;

id1=id;

output;

id2=id;

output;

id3=id;

output;

run;


Accepted Solutions
Solution
‎10-21-2016 09:37 PM
SAS Super FREQ
Posts: 8,743

Re: SAS PROGRAMMING QUESTION

Hi:
As I explained in my first post:
"In the first program, you read 4 observations and WORK.A has 4 observations and 1 variable or column called ID, as shown in the screen shot below. Your program that creates WORK.B has 3 explicit OUTPUT statements and makes 3 new variables, ID1, ID2, and, ID3. There are no controls in the program to restrict when OUTPUT occurs, so for EVERY observation in WORK.A, you will write 3 observations into WORK.B, for a total of 12 observations, as shown below. The variables in WORK.B are ID, ID1, ID2 and ID3, but for every row, the values are the same as for the original value of ID, so if you look at the PROC PRINT for WORK.B you will see that observations 1-3 are all for ID=1; observations 4-6 are all for ID=2; observations 7-9 are for ID=3 and observations 10-12 are for ID=4. I suspect this is not what you want."

The first program is writing out 3 observations for every single observation in work.a.

cynthia

View solution in original post


All Replies
SAS Super FREQ
Posts: 8,743

Re: SAS PROGRAMMING QUESTION

[ Edited ]

Hi:

  I am not sure what you are looking for when you ask for an ANSWER to your program. In the first program, you read 4 observations and WORK.A has 4 observations and 1 variable or column called ID, as shown in the screen shot below. Your program that creates WORK.B has 3 explicit OUTPUT statements and makes 3 new variables, ID1, ID2, and, ID3. There are no controls in the program to restrict when OUTPUT occurs, so for EVERY observation in WORK.A, you will write 3 observations into WORK.B, for a total of 12 observations, as shown below. The variables in WORK.B are ID, ID1, ID2 and ID3, but for every row, the values are the same as for the original value of ID, so if you look at the PROC PRINT for WORK.B you will see that observations 1-3 are all for ID=1; observations 4-6 are all for ID=2; observations 7-9 are for ID=3 and observations 10-12 are for ID=4. I suspect this is not what you want.

compare_a_b.png

 

  The program is working as written, so there is nothing to ANSWER. Is there something wrong with the output as shown in WORK.B? Did you need some other structure to the data.

 

  I wonder whether you wanted something more like this:

possible_alternative_transpose.png

Where the 4 observations from WORK.A were restructured to be only 1 observation with numbered values that corresponded to the original values from WORK.A (so ID1=1, ID2=2, etc).

 

There are 2 ways to restructure WORK.A -- one is to use PROC TRANSPOSE and the other is to use a DATA step program.

 

cynthai

Contributor
Posts: 20

Re: SAS PROGRAMMING QUESTION

Thank you for replying cynthai.

I know that code is perfectly right. I only want to know the logic behind
the output.

Thanks
Rohini S Das

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Solution
‎10-21-2016 09:37 PM
SAS Super FREQ
Posts: 8,743

Re: SAS PROGRAMMING QUESTION

Hi:
As I explained in my first post:
"In the first program, you read 4 observations and WORK.A has 4 observations and 1 variable or column called ID, as shown in the screen shot below. Your program that creates WORK.B has 3 explicit OUTPUT statements and makes 3 new variables, ID1, ID2, and, ID3. There are no controls in the program to restrict when OUTPUT occurs, so for EVERY observation in WORK.A, you will write 3 observations into WORK.B, for a total of 12 observations, as shown below. The variables in WORK.B are ID, ID1, ID2 and ID3, but for every row, the values are the same as for the original value of ID, so if you look at the PROC PRINT for WORK.B you will see that observations 1-3 are all for ID=1; observations 4-6 are all for ID=2; observations 7-9 are for ID=3 and observations 10-12 are for ID=4. I suspect this is not what you want."

The first program is writing out 3 observations for every single observation in work.a.

cynthia
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 264 views
  • 1 like
  • 2 in conversation