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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ
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

3 REPLIES 3
Cynthia_sas
SAS Super FREQ

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

ROHINISDAS
Obsidian | Level 7
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. -##
Cynthia_sas
SAS Super FREQ
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

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
  • 3 replies
  • 918 views
  • 1 like
  • 2 in conversation