Turn on suggestions

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

Showing results for

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

Options

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

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-25-2018 01:03 PM
(2043 views)

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;
```

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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,
;;;;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.