Solved
Contributor
Posts: 22

# SAS PROGRAMMING QUESTION

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: 9,364

## 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

All Replies
SAS Super FREQ
Posts: 9,364

## 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.

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:

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: 22

## Re: SAS PROGRAMMING QUESTION

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

Thanks
Rohini S Das

attachments. -##
Solution
‎10-21-2016 09:37 PM
SAS Super FREQ
Posts: 9,364

## 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.

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