Help using Base SAS procedures

Adding New Var AND new lines Based on Another Var values

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Adding New Var AND new lines Based on Another Var values

Hi everyone, is there an easy programmation way of 1) creating a new variable "NUTID", 2) then adding as many new lines to dataset as a previous variable "NUT" - how to go from left table in this attached excel to right end side table - PLEASE? 


Accepted Solutions
Solution
‎08-14-2017 04:04 PM
Super User
Posts: 13,508

Re: Adding New Var AND new lines Based on Another Var values

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

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.

 

Here is one way to had a variable that takes on values of 1 to n of an existing variable.

 

data have;
   input a b;
datalines;
1 2
3 4
5 3
;
run;

data want;
   set have;
   do nutid = 1 to a;
      output;
   end;
run;
   

The key is the output. This will create a new record each time that the Output statement executes. All other varaibles in the data set will duplicate.

 

View solution in original post


All Replies
Super User
Posts: 23,683

Re: Adding New Var AND new lines Based on Another Var values

1. Create a list of the values needed in each column

2. Create the 'big dataset' which is empty using cross join in SQL

3. Merge in initial table to fill the values.

 

 

Solution
‎08-14-2017 04:04 PM
Super User
Posts: 13,508

Re: Adding New Var AND new lines Based on Another Var values

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

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.

 

Here is one way to had a variable that takes on values of 1 to n of an existing variable.

 

data have;
   input a b;
datalines;
1 2
3 4
5 3
;
run;

data want;
   set have;
   do nutid = 1 to a;
      output;
   end;
run;
   

The key is the output. This will create a new record each time that the Output statement executes. All other varaibles in the data set will duplicate.

 

Contributor
Posts: 20

Re: Adding New Var AND new lines Based on Another Var values

Thank you kindly speedy useful user ;-0) Exactly what I wanted - TKS and ill look at how to post with no excel file next time ;-0) 

☑ This topic is solved.

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

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