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

Hi Team,

 

I want something very easy.

I want to mantain the same order of the variables of this dataset.

 

 

set output.&cons ;

 

 

I tried with retain and using keep statement and this does not mantain the same order of the variable names.

I read on the forums and they suggested to use Retain and Format

 

 

options validvarname=any;
data testa (drop=r1 r2 "Range Start"n "Range End"n) ;
format    
month_var $char32.
Range $char24.
"Current Count"n comma15.
"Current %"n percent12.2
"Previous Count"n comma15.
"Previous %"n percent12.2
"% Change"n percent12.2
; 
set output.&cons ;
r1=put(input("Range Start"n,15.),comma12.);
r2=put("Range End"n,comma15.);
Range=CATX('_',r1,r2);
;
run;
NOTE: Variable 'Previous Count'n is uninitialized.

 

 

getting this like Output:

 

month_varRangeCurrent CountCurrent %Previous CountPrevious %% Change Previous Count
M00_ACCT_BALANCE_AM._0313,313,11336.16%.33.97%2.19%2,323,333,131
M00_ACCT_BALANCE_AM1_100313,313,1135.58%.33.97%2.19%2,323,333,131

 

 

And I need this:

 

month_varRangeCurrent CountCurrent % Previous CountPrevious %% Change
M00_ACCT_BALANCE_AM._0313,313,11336.16%2,323,333,13133.97%2.19%
M00_ACCT_BALANCE_AM1_100313,313,1135.58%2,323,333,13133.97%2.19%
M00_ACCT_BALANCE_AM101_500313,313,11312.15%2,323,333,13133.97%2.19%

 

My question are 2:

 

1. Why is reading twice the variable "Previous Count" ? One with expected output and another with missing values

2.  Is there an easy way to mantain the same order of the variable names of my dataset ?

set output.&cons ;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Then just make sure your "set" statement precedes any statements referring to new variables.

 

The PDV (program data vector) will then have the original vars in original order on the left, and newly encountered vars to the right, in the order they are encountered by the sas compiler.

 

Edit:  Answer to 2nd question.  Your dataset probably already has a var named " Previous Count"n  (note the leading blank), but you are declaring another var "Previous Count"n   (no leading blank).  Two different variable names.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

Then just make sure your "set" statement precedes any statements referring to new variables.

 

The PDV (program data vector) will then have the original vars in original order on the left, and newly encountered vars to the right, in the order they are encountered by the sas compiler.

 

Edit:  Answer to 2nd question.  Your dataset probably already has a var named " Previous Count"n  (note the leading blank), but you are declaring another var "Previous Count"n   (no leading blank).  Two different variable names.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Jcorti
Obsidian | Level 7

Thanks Man!!

My variable Previous Count I don´t know why had leading spaces values,

" Previous Count"n comma15.

and it worked properly

 

Thanks again

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
  • 2 replies
  • 1391 views
  • 0 likes
  • 2 in conversation