DATA Step, Macro, Functions and more

Using the "DO UNTIL" loop to create long data set

Accepted Solution Solved
Reply
Contributor svh
Contributor
Posts: 21
Accepted Solution

Using the "DO UNTIL" loop to create long data set

I need to use iterative processing to create a data set with one row per College, per year, per Category value. I set the values to missing in this step. With the following code, I can simulate the data set for just one level of the "Category" variable (the value 'PR'), but I can't seem to figure out how to do this for the other two levels of category. Something tells me that I need to find a way to process a loop within the loop, but I can't find a resource to help with that. Does anyone have any tips?

 

data colleges; *This college variable has seven levels.;
input college1 $;
cards;
ORG_A
ORG_B
ORG_C
ORG_D
ORG_E
ORG_F
ORG_G
;
RUN;
PROC SORT DATA=COLLEGES;
BY COLLEGE1;
RUN;


data simulation1;
SET COLLEGES;
BY COLLEGE1;
year = 2012;
do until (year = 2017);
   year + 1;
   CATEGORY = 'PR'; *This creates an output data set with 35 observations, but CATEGORY has two other levels, so I need a data set with 105 observations;
   value = .;
   output;
END;
RUN;

Accepted Solutions
Solution
‎04-25-2018 02:01 PM
Super User
Posts: 6,933

Re: Using the "DO UNTIL" loop to create long data set

[ Edited ]

You're on the right track.  Here are some slight changes:

 


data simulation1;
SET COLLEGES;
BY COLLEGE1;

value = .;
do year = 2012 to 2017;

   do category='PR', 'AB', 'XY';   
      output;

   end;
END;
RUN;

 

You will have to fill in the actual values for CATEGORY.  Also, put the longest value first.  The first value in the list determines how many characters get used to store CATEGORY.

 

****************** EDITED:

 

Needs a slight change.  Should be:  do year = 2013 to 2017;

View solution in original post


All Replies
Super User
Posts: 13,941

Re: Using the "DO UNTIL" loop to create long data set

How about providing something that actually looks like your input data and the desired output?

It is very likely that Proc Transpose will do what you want.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Contributor svh
Contributor
Posts: 21

Re: Using the "DO UNTIL" loop to create long data set

Here is the final output data set. This data set needs to be reproduced for two other levels of the variable Category.

 

ata WORK.SIMULATION1;
  infile datalines dsd truncover;
  input college1:$8. year:32. CATEGORY:$2. value:32.;
datalines4;
ORG_A,2013,PR,
ORG_A,2014,PR,
ORG_A,2015,PR,
ORG_A,2016,PR,
ORG_A,2017,PR,
ORG_B,2013,PR,
ORG_B,2014,PR,
ORG_B,2015,PR,
ORG_B,2016,PR,
ORG_B,2017,PR,
ORG_C,2013,PR,
ORG_C,2014,PR,
ORG_C,2015,PR,
ORG_C,2016,PR,
ORG_C,2017,PR,
ORG_D,2013,PR,
ORG_D,2014,PR,
ORG_D,2015,PR,
ORG_D,2016,PR,
ORG_D,2017,PR,
ORG_E,2013,PR,
ORG_E,2014,PR,
ORG_E,2015,PR,
ORG_E,2016,PR,
ORG_E,2017,PR,
ORG_F,2013,PR,
ORG_F,2014,PR,
ORG_F,2015,PR,
ORG_F,2016,PR,
ORG_F,2017,PR,
ORG_G,2013,PR,
ORG_G,2014,PR,
ORG_G,2015,PR,
ORG_G,2016,PR,
ORG_G,2017,PR,
;;;;
Solution
‎04-25-2018 02:01 PM
Super User
Posts: 6,933

Re: Using the "DO UNTIL" loop to create long data set

[ Edited ]

You're on the right track.  Here are some slight changes:

 


data simulation1;
SET COLLEGES;
BY COLLEGE1;

value = .;
do year = 2012 to 2017;

   do category='PR', 'AB', 'XY';   
      output;

   end;
END;
RUN;

 

You will have to fill in the actual values for CATEGORY.  Also, put the longest value first.  The first value in the list determines how many characters get used to store CATEGORY.

 

****************** EDITED:

 

Needs a slight change.  Should be:  do year = 2013 to 2017;

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 139 views
  • 0 likes
  • 3 in conversation