turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Using the "DO UNTIL" loop to create long data set

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-25-2018 01:03 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-25-2018 01:23 PM - edited 04-25-2018 01:32 PM

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;

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-25-2018 01:13 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

04-25-2018 01:30 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-25-2018 01:23 PM - edited 04-25-2018 01:32 PM

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;